1 // SPDX-License-Identifier: GPL-2.0+
3 * Microchip PIC32 MUSB "glue layer"
5 * Copyright (C) 2015, Microchip Technology Inc.
9 * Based on the dsps "glue layer" code.
14 #include <dm/device_compat.h>
15 #include <linux/bitops.h>
16 #include <linux/delay.h>
17 #include <linux/usb/musb.h>
18 #include "linux-compat.h"
19 #include "musb_core.h"
20 #include "musb_uboot.h"
22 DECLARE_GLOBAL_DATA_PTR;
24 #define PIC32_TX_EP_MASK 0x0f /* EP0 + 7 Tx EPs */
25 #define PIC32_RX_EP_MASK 0x0e /* 7 Rx EPs */
27 #define MUSB_SOFTRST 0x7f
28 #define MUSB_SOFTRST_NRST BIT(0)
29 #define MUSB_SOFTRST_NRSTX BIT(1)
32 #define USBCRCON_USBWKUPEN BIT(0) /* Enable Wakeup Interrupt */
33 #define USBCRCON_USBRIE BIT(1) /* Enable Remote resume Interrupt */
34 #define USBCRCON_USBIE BIT(2) /* Enable USB General interrupt */
35 #define USBCRCON_SENDMONEN BIT(3) /* Enable Session End VBUS monitoring */
36 #define USBCRCON_BSVALMONEN BIT(4) /* Enable B-Device VBUS monitoring */
37 #define USBCRCON_ASVALMONEN BIT(5) /* Enable A-Device VBUS monitoring */
38 #define USBCRCON_VBUSMONEN BIT(6) /* Enable VBUS monitoring */
39 #define USBCRCON_PHYIDEN BIT(7) /* PHY ID monitoring enable */
40 #define USBCRCON_USBIDVAL BIT(8) /* USB ID value */
41 #define USBCRCON_USBIDOVEN BIT(9) /* USB ID override enable */
42 #define USBCRCON_USBWK BIT(24) /* USB Wakeup Status */
43 #define USBCRCON_USBRF BIT(25) /* USB Resume Status */
44 #define USBCRCON_USBIF BIT(26) /* USB General Interrupt Status */
46 /* PIC32 controller data */
47 struct pic32_musb_data {
48 struct musb_host_data mdata;
50 void __iomem *musb_glue;
53 #define to_pic32_musb_data(d) \
54 container_of(d, struct pic32_musb_data, dev)
56 static void pic32_musb_disable(struct musb *musb)
58 /* no way to shut the controller */
61 static int pic32_musb_enable(struct musb *musb)
63 /* soft reset by NRSTx */
64 musb_writeb(musb->mregs, MUSB_SOFTRST, MUSB_SOFTRST_NRSTX);
66 musb_platform_set_mode(musb, musb->board_mode);
71 static irqreturn_t pic32_interrupt(int irq, void *hci)
73 struct musb *musb = hci;
74 irqreturn_t ret = IRQ_NONE;
77 /* ack usb core interrupts */
78 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
80 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
82 /* ack endpoint interrupts */
83 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX) & PIC32_RX_EP_MASK;
85 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
87 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX) & PIC32_TX_EP_MASK;
89 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
91 /* drop spurious RX and TX if device is disconnected */
92 if (musb->int_usb & MUSB_INTR_DISCONNECT) {
97 if (musb->int_tx || musb->int_rx || musb->int_usb)
98 ret = musb_interrupt(musb);
103 static int pic32_musb_set_mode(struct musb *musb, u8 mode)
105 struct device *dev = musb->controller;
106 struct pic32_musb_data *pdata = to_pic32_musb_data(dev);
110 clrsetbits_le32(pdata->musb_glue + USBCRCON,
111 USBCRCON_USBIDVAL, USBCRCON_USBIDOVEN);
113 case MUSB_PERIPHERAL:
114 setbits_le32(pdata->musb_glue + USBCRCON,
115 USBCRCON_USBIDVAL | USBCRCON_USBIDOVEN);
118 dev_err(dev, "support for OTG is unimplemented\n");
121 dev_err(dev, "unsupported mode %d\n", mode);
128 static int pic32_musb_init(struct musb *musb)
130 struct pic32_musb_data *pdata = to_pic32_musb_data(musb->controller);
134 /* Returns zero if not clocked */
135 hwvers = musb_read_hwvers(musb->mregs);
140 power = musb_readb(musb->mregs, MUSB_POWER);
141 power = power | MUSB_POWER_RESET;
142 musb_writeb(musb->mregs, MUSB_POWER, power);
145 /* Start the on-chip PHY and its PLL. */
146 power = power & ~MUSB_POWER_RESET;
147 musb_writeb(musb->mregs, MUSB_POWER, power);
149 musb->isr = pic32_interrupt;
151 ctrl = USBCRCON_USBIF | USBCRCON_USBRF |
152 USBCRCON_USBWK | USBCRCON_USBIDOVEN |
153 USBCRCON_PHYIDEN | USBCRCON_USBIE |
154 USBCRCON_USBRIE | USBCRCON_USBWKUPEN |
156 writel(ctrl, pdata->musb_glue + USBCRCON);
161 /* PIC32 supports only 32bit read operation */
162 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
164 void __iomem *fifo = hw_ep->fifo;
165 u32 val, rem = len % 4;
167 /* USB stack ensures dst is always 32bit aligned. */
168 readsl(fifo, dst, len / 4);
171 val = musb_readl(fifo, 0);
172 memcpy(dst, &val, rem);
176 const struct musb_platform_ops pic32_musb_ops = {
177 .init = pic32_musb_init,
178 .set_mode = pic32_musb_set_mode,
179 .disable = pic32_musb_disable,
180 .enable = pic32_musb_enable,
183 /* PIC32 default FIFO config - fits in 8KB */
184 static struct musb_fifo_cfg pic32_musb_fifo_config[] = {
185 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
186 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
187 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
188 { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
189 { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
190 { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
191 { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
192 { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
193 { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
194 { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
195 { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
196 { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
197 { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
198 { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
201 static struct musb_hdrc_config pic32_musb_config = {
202 .fifo_cfg = pic32_musb_fifo_config,
203 .fifo_cfg_size = ARRAY_SIZE(pic32_musb_fifo_config),
210 /* PIC32 has one MUSB controller which can be host or gadget */
211 static struct musb_hdrc_platform_data pic32_musb_plat = {
213 .config = &pic32_musb_config,
214 .power = 250, /* 500mA */
215 .platform_ops = &pic32_musb_ops,
218 static int musb_usb_probe(struct udevice *dev)
220 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
221 struct pic32_musb_data *pdata = dev_get_priv(dev);
222 struct musb_host_data *mdata = &pdata->mdata;
223 struct fdt_resource mc, glue;
224 void *fdt = (void *)gd->fdt_blob;
225 int node = dev_of_offset(dev);
229 priv->desc_before_addr = true;
231 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
234 printf("pic32-musb: resource \"mc\" not found\n");
238 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
241 printf("pic32-musb: resource \"control\" not found\n");
245 mregs = ioremap(mc.start, fdt_resource_size(&mc));
246 pdata->musb_glue = ioremap(glue.start, fdt_resource_size(&glue));
248 /* init controller */
249 #ifdef CONFIG_USB_MUSB_HOST
250 mdata->host = musb_init_controller(&pic32_musb_plat,
255 ret = musb_lowlevel_init(mdata);
257 pic32_musb_plat.mode = MUSB_PERIPHERAL;
258 mdata->host = musb_register(&pic32_musb_plat, &pdata->dev, mregs);
262 if ((ret == 0) && mdata->host)
263 printf("PIC32 MUSB OTG\n");
268 static int musb_usb_remove(struct udevice *dev)
270 struct pic32_musb_data *pdata = dev_get_priv(dev);
272 musb_stop(pdata->mdata.host);
277 static const struct udevice_id pic32_musb_ids[] = {
278 { .compatible = "microchip,pic32mzda-usb" },
282 U_BOOT_DRIVER(usb_musb) = {
283 .name = "pic32-musb",
285 .of_match = pic32_musb_ids,
286 .probe = musb_usb_probe,
287 .remove = musb_usb_remove,
288 #ifdef CONFIG_USB_MUSB_HOST
289 .ops = &musb_usb_ops,
291 .plat_auto = sizeof(struct usb_platdata),
292 .priv_auto = sizeof(struct pic32_musb_data),