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.
13 #include <linux/usb/musb.h>
14 #include "linux-compat.h"
15 #include "musb_core.h"
16 #include "musb_uboot.h"
18 DECLARE_GLOBAL_DATA_PTR;
20 #define PIC32_TX_EP_MASK 0x0f /* EP0 + 7 Tx EPs */
21 #define PIC32_RX_EP_MASK 0x0e /* 7 Rx EPs */
23 #define MUSB_SOFTRST 0x7f
24 #define MUSB_SOFTRST_NRST BIT(0)
25 #define MUSB_SOFTRST_NRSTX BIT(1)
28 #define USBCRCON_USBWKUPEN BIT(0) /* Enable Wakeup Interrupt */
29 #define USBCRCON_USBRIE BIT(1) /* Enable Remote resume Interrupt */
30 #define USBCRCON_USBIE BIT(2) /* Enable USB General interrupt */
31 #define USBCRCON_SENDMONEN BIT(3) /* Enable Session End VBUS monitoring */
32 #define USBCRCON_BSVALMONEN BIT(4) /* Enable B-Device VBUS monitoring */
33 #define USBCRCON_ASVALMONEN BIT(5) /* Enable A-Device VBUS monitoring */
34 #define USBCRCON_VBUSMONEN BIT(6) /* Enable VBUS monitoring */
35 #define USBCRCON_PHYIDEN BIT(7) /* PHY ID monitoring enable */
36 #define USBCRCON_USBIDVAL BIT(8) /* USB ID value */
37 #define USBCRCON_USBIDOVEN BIT(9) /* USB ID override enable */
38 #define USBCRCON_USBWK BIT(24) /* USB Wakeup Status */
39 #define USBCRCON_USBRF BIT(25) /* USB Resume Status */
40 #define USBCRCON_USBIF BIT(26) /* USB General Interrupt Status */
42 /* PIC32 controller data */
43 struct pic32_musb_data {
44 struct musb_host_data mdata;
46 void __iomem *musb_glue;
49 #define to_pic32_musb_data(d) \
50 container_of(d, struct pic32_musb_data, dev)
52 static void pic32_musb_disable(struct musb *musb)
54 /* no way to shut the controller */
57 static int pic32_musb_enable(struct musb *musb)
59 /* soft reset by NRSTx */
60 musb_writeb(musb->mregs, MUSB_SOFTRST, MUSB_SOFTRST_NRSTX);
62 musb_platform_set_mode(musb, musb->board_mode);
67 static irqreturn_t pic32_interrupt(int irq, void *hci)
69 struct musb *musb = hci;
70 irqreturn_t ret = IRQ_NONE;
73 /* ack usb core interrupts */
74 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
76 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
78 /* ack endpoint interrupts */
79 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX) & PIC32_RX_EP_MASK;
81 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
83 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX) & PIC32_TX_EP_MASK;
85 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
87 /* drop spurious RX and TX if device is disconnected */
88 if (musb->int_usb & MUSB_INTR_DISCONNECT) {
93 if (musb->int_tx || musb->int_rx || musb->int_usb)
94 ret = musb_interrupt(musb);
99 static int pic32_musb_set_mode(struct musb *musb, u8 mode)
101 struct device *dev = musb->controller;
102 struct pic32_musb_data *pdata = to_pic32_musb_data(dev);
106 clrsetbits_le32(pdata->musb_glue + USBCRCON,
107 USBCRCON_USBIDVAL, USBCRCON_USBIDOVEN);
109 case MUSB_PERIPHERAL:
110 setbits_le32(pdata->musb_glue + USBCRCON,
111 USBCRCON_USBIDVAL | USBCRCON_USBIDOVEN);
114 dev_err(dev, "support for OTG is unimplemented\n");
117 dev_err(dev, "unsupported mode %d\n", mode);
124 static int pic32_musb_init(struct musb *musb)
126 struct pic32_musb_data *pdata = to_pic32_musb_data(musb->controller);
130 /* Returns zero if not clocked */
131 hwvers = musb_read_hwvers(musb->mregs);
136 power = musb_readb(musb->mregs, MUSB_POWER);
137 power = power | MUSB_POWER_RESET;
138 musb_writeb(musb->mregs, MUSB_POWER, power);
141 /* Start the on-chip PHY and its PLL. */
142 power = power & ~MUSB_POWER_RESET;
143 musb_writeb(musb->mregs, MUSB_POWER, power);
145 musb->isr = pic32_interrupt;
147 ctrl = USBCRCON_USBIF | USBCRCON_USBRF |
148 USBCRCON_USBWK | USBCRCON_USBIDOVEN |
149 USBCRCON_PHYIDEN | USBCRCON_USBIE |
150 USBCRCON_USBRIE | USBCRCON_USBWKUPEN |
152 writel(ctrl, pdata->musb_glue + USBCRCON);
157 /* PIC32 supports only 32bit read operation */
158 void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
160 void __iomem *fifo = hw_ep->fifo;
161 u32 val, rem = len % 4;
163 /* USB stack ensures dst is always 32bit aligned. */
164 readsl(fifo, dst, len / 4);
167 val = musb_readl(fifo, 0);
168 memcpy(dst, &val, rem);
172 const struct musb_platform_ops pic32_musb_ops = {
173 .init = pic32_musb_init,
174 .set_mode = pic32_musb_set_mode,
175 .disable = pic32_musb_disable,
176 .enable = pic32_musb_enable,
179 /* PIC32 default FIFO config - fits in 8KB */
180 static struct musb_fifo_cfg pic32_musb_fifo_config[] = {
181 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
182 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
183 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
184 { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
185 { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
186 { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
187 { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
188 { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
189 { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
190 { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
191 { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
192 { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
193 { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
194 { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
197 static struct musb_hdrc_config pic32_musb_config = {
198 .fifo_cfg = pic32_musb_fifo_config,
199 .fifo_cfg_size = ARRAY_SIZE(pic32_musb_fifo_config),
206 /* PIC32 has one MUSB controller which can be host or gadget */
207 static struct musb_hdrc_platform_data pic32_musb_plat = {
209 .config = &pic32_musb_config,
210 .power = 250, /* 500mA */
211 .platform_ops = &pic32_musb_ops,
214 static int musb_usb_probe(struct udevice *dev)
216 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
217 struct pic32_musb_data *pdata = dev_get_priv(dev);
218 struct musb_host_data *mdata = &pdata->mdata;
219 struct fdt_resource mc, glue;
220 void *fdt = (void *)gd->fdt_blob;
221 int node = dev_of_offset(dev);
225 priv->desc_before_addr = true;
227 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
230 printf("pic32-musb: resource \"mc\" not found\n");
234 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
237 printf("pic32-musb: resource \"control\" not found\n");
241 mregs = ioremap(mc.start, fdt_resource_size(&mc));
242 pdata->musb_glue = ioremap(glue.start, fdt_resource_size(&glue));
244 /* init controller */
245 #ifdef CONFIG_USB_MUSB_HOST
246 mdata->host = musb_init_controller(&pic32_musb_plat,
251 ret = musb_lowlevel_init(mdata);
253 pic32_musb_plat.mode = MUSB_PERIPHERAL;
254 mdata->host = musb_register(&pic32_musb_plat, &pdata->dev, mregs);
258 if ((ret == 0) && mdata->host)
259 printf("PIC32 MUSB OTG\n");
264 static int musb_usb_remove(struct udevice *dev)
266 struct pic32_musb_data *pdata = dev_get_priv(dev);
268 musb_stop(pdata->mdata.host);
273 static const struct udevice_id pic32_musb_ids[] = {
274 { .compatible = "microchip,pic32mzda-usb" },
278 U_BOOT_DRIVER(usb_musb) = {
279 .name = "pic32-musb",
281 .of_match = pic32_musb_ids,
282 .probe = musb_usb_probe,
283 .remove = musb_usb_remove,
284 #ifdef CONFIG_USB_MUSB_HOST
285 .ops = &musb_usb_ops,
287 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
288 .priv_auto_alloc_size = sizeof(struct pic32_musb_data),