1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <linux/usb/musb.h>
15#include "linux-compat.h"
16#include "musb_core.h"
17#include "musb_uboot.h"
18
19DECLARE_GLOBAL_DATA_PTR;
20
21#define PIC32_TX_EP_MASK 0x0f
22#define PIC32_RX_EP_MASK 0x0e
23
24#define MUSB_SOFTRST 0x7f
25#define MUSB_SOFTRST_NRST BIT(0)
26#define MUSB_SOFTRST_NRSTX BIT(1)
27
28#define USBCRCON 0
29#define USBCRCON_USBWKUPEN BIT(0)
30#define USBCRCON_USBRIE BIT(1)
31#define USBCRCON_USBIE BIT(2)
32#define USBCRCON_SENDMONEN BIT(3)
33#define USBCRCON_BSVALMONEN BIT(4)
34#define USBCRCON_ASVALMONEN BIT(5)
35#define USBCRCON_VBUSMONEN BIT(6)
36#define USBCRCON_PHYIDEN BIT(7)
37#define USBCRCON_USBIDVAL BIT(8)
38#define USBCRCON_USBIDOVEN BIT(9)
39#define USBCRCON_USBWK BIT(24)
40#define USBCRCON_USBRF BIT(25)
41#define USBCRCON_USBIF BIT(26)
42
43
44struct pic32_musb_data {
45 struct musb_host_data mdata;
46 struct device dev;
47 void __iomem *musb_glue;
48};
49
50#define to_pic32_musb_data(d) \
51 container_of(d, struct pic32_musb_data, dev)
52
53static void pic32_musb_disable(struct musb *musb)
54{
55
56}
57
58static int pic32_musb_enable(struct musb *musb)
59{
60
61 musb_writeb(musb->mregs, MUSB_SOFTRST, MUSB_SOFTRST_NRSTX);
62
63 musb_platform_set_mode(musb, musb->board_mode);
64
65 return 0;
66}
67
68static irqreturn_t pic32_interrupt(int irq, void *hci)
69{
70 struct musb *musb = hci;
71 irqreturn_t ret = IRQ_NONE;
72 u32 epintr, usbintr;
73
74
75 musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
76 if (musb->int_usb)
77 musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
78
79
80 musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX) & PIC32_RX_EP_MASK;
81 if (musb->int_rx)
82 musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
83
84 musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX) & PIC32_TX_EP_MASK;
85 if (musb->int_tx)
86 musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
87
88
89 if (musb->int_usb & MUSB_INTR_DISCONNECT) {
90 musb->int_tx = 0;
91 musb->int_rx = 0;
92 }
93
94 if (musb->int_tx || musb->int_rx || musb->int_usb)
95 ret = musb_interrupt(musb);
96
97 return ret;
98}
99
100static int pic32_musb_set_mode(struct musb *musb, u8 mode)
101{
102 struct device *dev = musb->controller;
103 struct pic32_musb_data *pdata = to_pic32_musb_data(dev);
104
105 switch (mode) {
106 case MUSB_HOST:
107 clrsetbits_le32(pdata->musb_glue + USBCRCON,
108 USBCRCON_USBIDVAL, USBCRCON_USBIDOVEN);
109 break;
110 case MUSB_PERIPHERAL:
111 setbits_le32(pdata->musb_glue + USBCRCON,
112 USBCRCON_USBIDVAL | USBCRCON_USBIDOVEN);
113 break;
114 case MUSB_OTG:
115 dev_err(dev, "support for OTG is unimplemented\n");
116 break;
117 default:
118 dev_err(dev, "unsupported mode %d\n", mode);
119 return -EINVAL;
120 }
121
122 return 0;
123}
124
125static int pic32_musb_init(struct musb *musb)
126{
127 struct pic32_musb_data *pdata = to_pic32_musb_data(musb->controller);
128 u32 ctrl, hwvers;
129 u8 power;
130
131
132 hwvers = musb_read_hwvers(musb->mregs);
133 if (!hwvers)
134 return -ENODEV;
135
136
137 power = musb_readb(musb->mregs, MUSB_POWER);
138 power = power | MUSB_POWER_RESET;
139 musb_writeb(musb->mregs, MUSB_POWER, power);
140 mdelay(100);
141
142
143 power = power & ~MUSB_POWER_RESET;
144 musb_writeb(musb->mregs, MUSB_POWER, power);
145
146 musb->isr = pic32_interrupt;
147
148 ctrl = USBCRCON_USBIF | USBCRCON_USBRF |
149 USBCRCON_USBWK | USBCRCON_USBIDOVEN |
150 USBCRCON_PHYIDEN | USBCRCON_USBIE |
151 USBCRCON_USBRIE | USBCRCON_USBWKUPEN |
152 USBCRCON_VBUSMONEN;
153 writel(ctrl, pdata->musb_glue + USBCRCON);
154
155 return 0;
156}
157
158
159void musb_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
160{
161 void __iomem *fifo = hw_ep->fifo;
162 u32 val, rem = len % 4;
163
164
165 readsl(fifo, dst, len / 4);
166 if (rem) {
167 dst += len & ~0x03;
168 val = musb_readl(fifo, 0);
169 memcpy(dst, &val, rem);
170 }
171}
172
173const struct musb_platform_ops pic32_musb_ops = {
174 .init = pic32_musb_init,
175 .set_mode = pic32_musb_set_mode,
176 .disable = pic32_musb_disable,
177 .enable = pic32_musb_enable,
178};
179
180
181static struct musb_fifo_cfg pic32_musb_fifo_config[] = {
182 { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
183 { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
184 { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
185 { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
186 { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
187 { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
188 { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
189 { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
190 { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
191 { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
192 { .hw_ep_num = 6, .style = FIFO_TX, .maxpacket = 512, },
193 { .hw_ep_num = 6, .style = FIFO_RX, .maxpacket = 512, },
194 { .hw_ep_num = 7, .style = FIFO_TX, .maxpacket = 512, },
195 { .hw_ep_num = 7, .style = FIFO_RX, .maxpacket = 512, },
196};
197
198static struct musb_hdrc_config pic32_musb_config = {
199 .fifo_cfg = pic32_musb_fifo_config,
200 .fifo_cfg_size = ARRAY_SIZE(pic32_musb_fifo_config),
201 .multipoint = 1,
202 .dyn_fifo = 1,
203 .num_eps = 8,
204 .ram_bits = 11,
205};
206
207
208static struct musb_hdrc_platform_data pic32_musb_plat = {
209 .mode = MUSB_HOST,
210 .config = &pic32_musb_config,
211 .power = 250,
212 .platform_ops = &pic32_musb_ops,
213};
214
215static int musb_usb_probe(struct udevice *dev)
216{
217 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
218 struct pic32_musb_data *pdata = dev_get_priv(dev);
219 struct musb_host_data *mdata = &pdata->mdata;
220 struct fdt_resource mc, glue;
221 void *fdt = (void *)gd->fdt_blob;
222 int node = dev->of_offset;
223 void __iomem *mregs;
224 int ret;
225
226 priv->desc_before_addr = true;
227
228 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
229 "mc", &mc);
230 if (ret < 0) {
231 printf("pic32-musb: resource \"mc\" not found\n");
232 return ret;
233 }
234
235 ret = fdt_get_named_resource(fdt, node, "reg", "reg-names",
236 "control", &glue);
237 if (ret < 0) {
238 printf("pic32-musb: resource \"control\" not found\n");
239 return ret;
240 }
241
242 mregs = ioremap(mc.start, fdt_resource_size(&mc));
243 pdata->musb_glue = ioremap(glue.start, fdt_resource_size(&glue));
244
245
246#ifdef CONFIG_USB_MUSB_HOST
247 mdata->host = musb_init_controller(&pic32_musb_plat,
248 &pdata->dev, mregs);
249 if (!mdata->host)
250 return -EIO;
251
252 ret = musb_lowlevel_init(mdata);
253#else
254 pic32_musb_plat.mode = MUSB_PERIPHERAL;
255 ret = musb_register(&pic32_musb_plat, &pdata->dev, mregs);
256#endif
257 if (ret == 0)
258 printf("PIC32 MUSB OTG\n");
259
260 return ret;
261}
262
263static int musb_usb_remove(struct udevice *dev)
264{
265 struct pic32_musb_data *pdata = dev_get_priv(dev);
266
267 musb_stop(pdata->mdata.host);
268
269 return 0;
270}
271
272static const struct udevice_id pic32_musb_ids[] = {
273 { .compatible = "microchip,pic32mzda-usb" },
274 { }
275};
276
277U_BOOT_DRIVER(usb_musb) = {
278 .name = "pic32-musb",
279 .id = UCLASS_USB,
280 .of_match = pic32_musb_ids,
281 .probe = musb_usb_probe,
282 .remove = musb_usb_remove,
283#ifdef CONFIG_USB_MUSB_HOST
284 .ops = &musb_usb_ops,
285#endif
286 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
287 .priv_auto_alloc_size = sizeof(struct pic32_musb_data),
288};
289