Rui Miguel Silva | 88861a2 | 2022-06-29 11:06:15 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright 2021 Linaro, Rui Miguel Silva <rui.silva@linaro.org> |
| 4 | * |
| 5 | * based on original code from: |
| 6 | * (c) 2007 Sebastian Siewior <bigeasy@linutronix.de> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <dm/device-internal.h> |
| 12 | #include <dm/device_compat.h> |
| 13 | #include <dm/devres.h> |
| 14 | #include <dm/lists.h> |
| 15 | #include <linux/bug.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/usb/otg.h> |
| 19 | #include <log.h> |
| 20 | #include <usb.h> |
| 21 | |
| 22 | #include "isp1760-core.h" |
| 23 | #include "isp1760-regs.h" |
| 24 | #include "isp1760-uboot.h" |
| 25 | |
| 26 | static int isp1760_of_to_plat(struct udevice *dev) |
| 27 | { |
| 28 | struct isp1760_device *isp = dev_get_plat(dev); |
| 29 | unsigned int devflags = 0; |
| 30 | u32 bus_width = 0; |
| 31 | ofnode dp; |
| 32 | |
| 33 | if (!dev_has_ofnode(dev)) { |
| 34 | /* select isp1763 as the default device */ |
| 35 | devflags = ISP1760_FLAG_ISP1763 | ISP1760_FLAG_BUS_WIDTH_16; |
| 36 | pr_err("isp1760: no platform data\n"); |
| 37 | goto isp_setup; |
| 38 | } |
| 39 | |
| 40 | dp = dev_ofnode(dev); |
| 41 | |
| 42 | if (ofnode_device_is_compatible(dp, "nxp,usb-isp1761")) |
| 43 | devflags |= ISP1760_FLAG_ISP1761; |
| 44 | |
| 45 | if (ofnode_device_is_compatible(dp, "nxp,usb-isp1763")) |
| 46 | devflags |= ISP1760_FLAG_ISP1763; |
| 47 | |
| 48 | /* |
| 49 | * Some systems wire up only 8 of 16 data lines or |
| 50 | * 16 of the 32 data lines |
| 51 | */ |
| 52 | bus_width = ofnode_read_u32_default(dp, "bus-width", 16); |
| 53 | if (bus_width == 16) |
| 54 | devflags |= ISP1760_FLAG_BUS_WIDTH_16; |
| 55 | else if (bus_width == 8) |
| 56 | devflags |= ISP1760_FLAG_BUS_WIDTH_8; |
| 57 | |
| 58 | if (usb_get_dr_mode(dev_ofnode(dev)) == USB_DR_MODE_PERIPHERAL) |
| 59 | devflags |= ISP1760_FLAG_PERIPHERAL_EN; |
| 60 | |
| 61 | if (ofnode_read_bool(dp, "analog-oc")) |
| 62 | devflags |= ISP1760_FLAG_ANALOG_OC; |
| 63 | |
| 64 | if (ofnode_read_bool(dp, "dack-polarity")) |
| 65 | devflags |= ISP1760_FLAG_DACK_POL_HIGH; |
| 66 | |
| 67 | if (ofnode_read_bool(dp, "dreq-polarity")) |
| 68 | devflags |= ISP1760_FLAG_DREQ_POL_HIGH; |
| 69 | |
| 70 | isp_setup: |
| 71 | isp->devflags = devflags; |
| 72 | isp->dev = dev; |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | static int isp1760_plat_probe(struct udevice *dev) |
| 78 | { |
| 79 | struct isp1760_device *isp = dev_get_plat(dev); |
| 80 | struct resource mem_res; |
| 81 | struct resource irq_res; |
| 82 | int ret; |
| 83 | |
| 84 | dev_read_resource(dev, 0, &mem_res); |
| 85 | dev_read_resource(dev, 1, &irq_res); |
| 86 | |
| 87 | isp1760_init_kmem_once(); |
| 88 | |
| 89 | ret = isp1760_register(isp, &mem_res, irq_res.start, irq_res.flags); |
| 90 | if (ret < 0) { |
| 91 | isp1760_deinit_kmem_cache(); |
| 92 | return ret; |
| 93 | } |
| 94 | |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int isp1760_plat_remove(struct udevice *dev) |
| 99 | { |
| 100 | struct isp1760_device *isp = dev_get_plat(dev); |
| 101 | |
| 102 | isp1760_deinit_kmem_cache(); |
| 103 | isp1760_unregister(isp); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static const struct udevice_id isp1760_ids[] = { |
| 109 | { .compatible = "nxp,usb-isp1760", }, |
| 110 | { .compatible = "nxp,usb-isp1761", }, |
| 111 | { .compatible = "nxp,usb-isp1763", }, |
| 112 | { }, |
| 113 | }; |
| 114 | |
| 115 | U_BOOT_DRIVER(isp1760) = { |
| 116 | .name = "isp1760", |
| 117 | .id = UCLASS_USB, |
| 118 | .of_match = isp1760_ids, |
| 119 | .of_to_plat = isp1760_of_to_plat, |
| 120 | .ops = &isp1760_usb_ops, |
| 121 | .probe = isp1760_plat_probe, |
| 122 | .remove = isp1760_plat_remove, |
| 123 | .plat_auto = sizeof(struct isp1760_device), |
| 124 | .priv_auto = sizeof(struct isp1760_host_data), |
| 125 | }; |