1 // SPDX-License-Identifier: GPL-2.0+
3 * NetChip 2280 high/full speed USB device controller.
4 * Unlike many such controllers, this one talks PCI.
8 * Copyright (C) 2002 NetChip Technology, Inc. (http://www.netchip.com)
9 * Copyright (C) 2003 David Brownell
10 * Copyright (C) 2014 Ricardo Ribalda - Qtechnology/AS
13 #include <linux/usb/net2280.h>
14 #include <linux/usb/usb338x.h>
16 /*-------------------------------------------------------------------------*/
20 /* indexed registers [11.10] are accessed indirectly
21 * caller must own the device lock.
24 static inline u32
get_idx_reg(struct net2280_regs __iomem
*regs
, u32 index
)
26 writel(index
, ®s
->idxaddr
);
27 /* NOTE: synchs device/cpu memory views */
28 return readl(®s
->idxdata
);
32 set_idx_reg(struct net2280_regs __iomem
*regs
, u32 index
, u32 value
)
34 writel(index
, ®s
->idxaddr
);
35 writel(value
, ®s
->idxdata
);
36 /* posted, may not be visible yet */
39 #endif /* __KERNEL__ */
41 #define PCI_VENDOR_ID_PLX_LEGACY 0x17cc
43 #define PLX_LEGACY BIT(0)
44 #define PLX_2280 BIT(1)
45 #define PLX_SUPERSPEED BIT(2)
46 #define PLX_PCIE BIT(3)
49 #define RETRY_COUNTER 16
50 #define FORCE_PCI_SERR 11
51 #define FORCE_PCI_INTERRUPT 10
52 #define FORCE_USB_INTERRUPT 9
53 #define FORCE_CPU_INTERRUPT 8
54 #define ILLEGAL_BYTE_ENABLES 5
56 #define FORCE_RECEIVE_ERROR 2
57 #define FORCE_TRANSMIT_CRC_ERROR 0
58 #define REG_FRAME 0x02 /* from last sof */
59 #define REG_CHIPREV 0x03 /* in bcd */
60 #define REG_HS_NAK_RATE 0x0a /* NAK per N uframes */
62 #define CHIPREV_1 0x0100
63 #define CHIPREV_1A 0x0110
66 #define DEFECT_7374_NUMBEROF_MAX_WAIT_LOOPS 200
67 #define DEFECT_7374_PROCESSOR_WAIT_TIME 10
69 /* ep0 max packet size */
70 #define EP0_SS_MAX_PACKET_SIZE 0x200
71 #define EP0_HS_MAX_PACKET_SIZE 0x40
74 /*-------------------------------------------------------------------------*/
76 /* [8.3] for scatter/gather i/o
77 * use struct net2280_dma_regs bitfields
81 __le32 dmaaddr
; /* the buffer */
82 __le32 dmadesc
; /* next dma descriptor */
86 /*-------------------------------------------------------------------------*/
88 /* DRIVER DATA STRUCTURES and UTILITIES */
92 struct net2280_ep_regs __iomem
*cfg
;
93 struct net2280_ep_regs __iomem
*regs
;
94 struct net2280_dma_regs __iomem
*dma
;
95 struct net2280_dma
*dummy
;
96 dma_addr_t td_dma
; /* of dummy */
100 /* analogous to a host-side qh */
101 struct list_head queue
;
102 const struct usb_endpoint_descriptor
*desc
;
105 in_fifo_validate
: 1,
114 static inline void allow_status(struct net2280_ep
*ep
)
117 writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
) |
118 BIT(CLEAR_NAK_OUT_PACKETS
) |
119 BIT(CLEAR_NAK_OUT_PACKETS_MODE
),
124 static inline void allow_status_338x(struct net2280_ep
*ep
)
127 * Control Status Phase Handshake was set by the chip when the setup
128 * packet arrived. While set, the chip automatically NAKs the host's
129 * Status Phase tokens.
131 writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
), &ep
->regs
->ep_rsp
);
135 /* TD 9.9 Halt Endpoint test. TD 9.22 set feature test. */
139 struct net2280_request
{
140 struct usb_request req
;
141 struct net2280_dma
*td
;
143 struct list_head queue
;
149 /* each pci device provides one gadget, several endpoints */
150 struct usb_gadget gadget
;
152 struct net2280_ep ep
[9];
153 struct usb_gadget_driver
*driver
;
154 unsigned enabled
: 1,
168 kernel_ulong_t quirks
;
171 /* pci state used to access those endpoints */
172 struct pci_dev
*pdev
;
173 struct net2280_regs __iomem
*regs
;
174 struct net2280_usb_regs __iomem
*usb
;
175 struct usb338x_usb_ext_regs __iomem
*usb_ext
;
176 struct net2280_pci_regs __iomem
*pci
;
177 struct net2280_dma_regs __iomem
*dma
;
178 struct net2280_dep_regs __iomem
*dep
;
179 struct net2280_ep_regs __iomem
*epregs
;
180 struct usb338x_ll_regs __iomem
*llregs
;
181 struct usb338x_ll_lfps_regs __iomem
*ll_lfps_regs
;
182 struct usb338x_ll_tsn_regs __iomem
*ll_tsn_regs
;
183 struct usb338x_ll_chi_regs __iomem
*ll_chicken_reg
;
184 struct usb338x_pl_regs __iomem
*plregs
;
186 struct dma_pool
*requests
;
190 static inline void set_halt(struct net2280_ep
*ep
)
192 /* ep0 and bulk/intr endpoints */
193 writel(BIT(CLEAR_CONTROL_STATUS_PHASE_HANDSHAKE
) |
194 /* set NAK_OUT for erratum 0114 */
195 ((ep
->dev
->chiprev
== CHIPREV_1
) << SET_NAK_OUT_PACKETS
) |
196 BIT(SET_ENDPOINT_HALT
),
200 static inline void clear_halt(struct net2280_ep
*ep
)
202 /* ep0 and bulk/intr endpoints */
203 writel(BIT(CLEAR_ENDPOINT_HALT
) |
204 BIT(CLEAR_ENDPOINT_TOGGLE
) |
206 * unless the gadget driver left a short packet in the
207 * fifo, this reverses the erratum 0114 workaround.
209 ((ep
->dev
->chiprev
== CHIPREV_1
) << CLEAR_NAK_OUT_PACKETS
),
214 * FSM value for Defect 7374 (U1U2 Test) is managed in
215 * chip's SCRATCH register:
217 #define DEFECT7374_FSM_FIELD 28
219 /* Waiting for Control Read:
220 * - A transition to this state indicates a fresh USB connection,
221 * before the first Setup Packet. The connection speed is not
222 * known. Firmware is waiting for the first Control Read.
223 * - Starting state: This state can be thought of as the FSM's typical
225 * - Tip: Upon the first SS Control Read the FSM never
226 * returns to this state.
228 #define DEFECT7374_FSM_WAITING_FOR_CONTROL_READ BIT(DEFECT7374_FSM_FIELD)
230 /* Non-SS Control Read:
231 * - A transition to this state indicates detection of the first HS
232 * or FS Control Read.
233 * - Tip: Upon the first SS Control Read the FSM never
234 * returns to this state.
236 #define DEFECT7374_FSM_NON_SS_CONTROL_READ (2 << DEFECT7374_FSM_FIELD)
239 * - A transition to this state indicates detection of the
240 * first SS Control Read.
241 * - This state indicates workaround completion. Workarounds no longer
242 * need to be applied (as long as the chip remains powered up).
243 * - Tip: Once in this state the FSM state does not change (until
244 * the chip's power is lost and restored).
245 * - This can be thought of as the final state of the FSM;
246 * the FSM 'locks-up' in this state until the chip loses power.
248 #define DEFECT7374_FSM_SS_CONTROL_READ (3 << DEFECT7374_FSM_FIELD)
252 static inline void net2280_led_init(struct net2280
*dev
)
254 /* LED3 (green) is on during USB activity. note erratum 0113. */
255 writel(BIT(GPIO3_LED_SELECT
) |
256 BIT(GPIO3_OUTPUT_ENABLE
) |
257 BIT(GPIO2_OUTPUT_ENABLE
) |
258 BIT(GPIO1_OUTPUT_ENABLE
) |
259 BIT(GPIO0_OUTPUT_ENABLE
),
260 &dev
->regs
->gpioctl
);
263 /* indicate speed with bi-color LED 0/1 */
265 void net2280_led_speed(struct net2280
*dev
, enum usb_device_speed speed
)
267 u32 val
= readl(&dev
->regs
->gpioctl
);
269 case USB_SPEED_SUPER
: /* green + red */
270 val
|= BIT(GPIO0_DATA
) | BIT(GPIO1_DATA
);
272 case USB_SPEED_HIGH
: /* green */
273 val
&= ~BIT(GPIO0_DATA
);
274 val
|= BIT(GPIO1_DATA
);
276 case USB_SPEED_FULL
: /* red */
277 val
&= ~BIT(GPIO1_DATA
);
278 val
|= BIT(GPIO0_DATA
);
280 default: /* (off/black) */
281 val
&= ~(BIT(GPIO1_DATA
) | BIT(GPIO0_DATA
));
284 writel(val
, &dev
->regs
->gpioctl
);
287 /* indicate power with LED 2 */
288 static inline void net2280_led_active(struct net2280
*dev
, int is_active
)
290 u32 val
= readl(&dev
->regs
->gpioctl
);
292 /* FIXME this LED never seems to turn on.*/
297 writel(val
, &dev
->regs
->gpioctl
);
300 static inline void net2280_led_shutdown(struct net2280
*dev
)
302 /* turn off all four GPIO*_DATA bits */
303 writel(readl(&dev
->regs
->gpioctl
) & ~0x0f,
304 &dev
->regs
->gpioctl
);
309 #define net2280_led_init(dev) do { } while (0)
310 #define net2280_led_speed(dev, speed) do { } while (0)
311 #define net2280_led_shutdown(dev) do { } while (0)
315 /*-------------------------------------------------------------------------*/
317 #define ep_dbg(ndev, fmt, args...) \
318 dev_dbg((&((ndev)->pdev->dev)), fmt, ##args)
320 #define ep_vdbg(ndev, fmt, args...) \
321 dev_vdbg((&((ndev)->pdev->dev)), fmt, ##args)
323 #define ep_info(ndev, fmt, args...) \
324 dev_info((&((ndev)->pdev->dev)), fmt, ##args)
326 #define ep_warn(ndev, fmt, args...) \
327 dev_warn((&((ndev)->pdev->dev)), fmt, ##args)
329 #define ep_err(ndev, fmt, args...) \
330 dev_err((&((ndev)->pdev->dev)), fmt, ##args)
332 /*-------------------------------------------------------------------------*/
334 static inline void set_fifo_bytecount(struct net2280_ep
*ep
, unsigned count
)
336 if (ep
->dev
->pdev
->vendor
== 0x17cc)
337 writeb(count
, 2 + (u8 __iomem
*) &ep
->regs
->ep_cfg
);
339 u32 tmp
= readl(&ep
->cfg
->ep_cfg
) &
340 (~(0x07 << EP_FIFO_BYTE_COUNT
));
341 writel(tmp
| (count
<< EP_FIFO_BYTE_COUNT
), &ep
->cfg
->ep_cfg
);
345 static inline void start_out_naking(struct net2280_ep
*ep
)
347 /* NOTE: hardware races lurk here, and PING protocol issues */
348 writel(BIT(SET_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
349 /* synch with device */
350 readl(&ep
->regs
->ep_rsp
);
353 static inline void stop_out_naking(struct net2280_ep
*ep
)
357 tmp
= readl(&ep
->regs
->ep_stat
);
358 if ((tmp
& BIT(NAK_OUT_PACKETS
)) != 0)
359 writel(BIT(CLEAR_NAK_OUT_PACKETS
), &ep
->regs
->ep_rsp
);
363 static inline void set_max_speed(struct net2280_ep
*ep
, u32 max
)
366 static const u32 ep_enhanced
[9] = { 0x10, 0x60, 0x30, 0x80,
367 0x50, 0x20, 0x70, 0x40, 0x90 };
369 if (ep
->dev
->enhanced_mode
) {
370 reg
= ep_enhanced
[ep
->num
];
371 switch (ep
->dev
->gadget
.speed
) {
372 case USB_SPEED_SUPER
:
383 reg
= (ep
->num
+ 1) * 0x10;
384 if (ep
->dev
->gadget
.speed
!= USB_SPEED_HIGH
)
388 set_idx_reg(ep
->dev
->regs
, reg
, max
);
391 #endif /* __KERNEL__ */