1StrongARM SA-1100 USB function Driver
2Ward Willats <ward.willats@extenex.com> -  08Mar01
3
41. History
5''''''''''
6Brad Parker <brad@parker.boston.ma.us> ported the DEC/Compaq "Itsy"
7SA-1100 USB Function driver to the 2.4.x code base in late 2000, for
8use as an "ethernet over usb" link. His original notes are here in
9section 4. Nicolas Pitre <nico@cam.org> rewrote the transmitter and
10reciver (endpoints 1 and 2) in late 2000 to use the standard SA DMA
11API and I added a bulk character interface and reworked the control
12control module code and rewrote endpoint zero in early 2001.
13
14This release (22Feb01) is the first that completely separates
15client modules (usb-eth.c and usb-char.c) from the SA-1100 USB core.
16(usb_ctl, usb_ep0, usb_send and usb_receive)and makes the whole
17mess a module. Oleg Drokin has done a huge amount of work, fixing
18things I break and adding support for the generic usbnet driver
19from the AC tree.
20
212. Usage
22''''''''
23Turn on CONFIG_SA1100_USB_NETLINK to use the "ethernet over usb"
24functionality. Turn it off to use the character oriented
25interface. The character driver currently uses mknod c 10 240.
26
27Programming:
28The public interface is in sa1100_usb.h. For a client USB service
29to use the SA-1100 USB core driver it should:
30
311. Call sa1100_usb_open() to get the usb core assigned to it.
32
332. Setup descriptors as appropriate for the task at hand. Esp.
34important are endpoint max packet lengths, vendor and product IDs,
35and type of endpoint (bulk or interrupt). Call
36sa1100_get_descriptor_ptr() to get this.
37
383. Call sa1100_usb_start() to actually start the usb hardware.
39At this time the host will configure the device.
40
41...at shutdown...
42
434. Call sa1100_usb_stop() to stop the USB core.
445. Call sa1100_usb_close() to free the core for use by another
45client.
46
473. Netlink Usage
48''''''''''''''''
49StrongARM SA-1100 USB function "ethernet over usb driver"
50Brad Parker <brad@parker.boston.ma.us>
51
52I ported the DEC "Itsy" usb "ethernet over usb driver" code to the 2.4.x
53base and made some enhacements and bug fixes.  This code has 2 sides and
54implements a simple "ethernet over usb" functionality.
55
56function (SA1100) side:
57- the driver has two endpoints and uses interrupt and bulk transfer to
58receive/send packets.  the driver does not require any other usb code
59and should work on most any sa1100.
60
61host (SA1111) side:
62- because the SA1111 usb host is not working yet I tested this driver
63(usb-net-host.c) on a 2.2.14 based PC with the latest usb backport.
64It has been fully converted to use URBs and worked well with my UHCI
65based controller.
66
67TESTING:
68
69To test you need an assabet on the 'function' side, a PC on the 'host'
70side and a USB A-B cable to connect them together.
71
72Boot a kernel on the assabet with "USB function and net-host support"
73(CONFIG_SA1100_USB) turned on.  This will define an interface named
74"usbf".  Once it's booted you can setup the interface with
75
76	mount -t proc /proc /proc
77	/sbin/ifconfig usbf 1.1.1.2
78
79I used a 2.2.14 kernel on a x86 PC for the host side.  It has a built
80in UHCI usb controller chip.  I installed the latest USB backport from
81http://www.linux-usb.org onto the 2.2.14 kernel sources and turned on
82"USB net-host" (CONFIG_USB_NET_HOST) as a module.  Load the module
83"usb-net-host.o" and connect the USB cable to the assabet.  Configure
84the usb network interface with
85
86	/sbin/ifconfig usb0 1.1.1.1
87
88You should be able to "ping" the assabet now with
89
90	ping -c 1 1.1.1.2
91
92If the assabet is running inetd the usual network services such as
93telnet and ftp should work.
94
95Oleg Drokin in 2.4.2-rmk1-np2 (08Mar01) added module config params for
96read and write size to the usb-eth.c client to allow dynamic setting
97of the DATA0/DATA1 packet size on the usb wire:
98
99usb_rsize - number of bytes in OUT packets from host to SA1100
100usb_wsize - number of bytes in IN packets from SA1100 to host
101
102This allows dynamic tuning for performance or to prevent overruning
103the the host with data.
104
1054. Known Issues
106'''''''''''''''
107- We are fiddling with various ways to set the IMP register in
108usb_send.c. A small percentage of the time, this value does not
109"take."
110
111- I've started to bring back the /proc interface, but clients
112of the sa-usb core currently don't have a directory or something
113to put their stats into.
114
115- Only a useful subset of ep0 setup calls have been implemented.
116
117
1185. Mysteries of the Universe
119''''''''''''''''''''''''''''
120This driver has been hard to develop because the documentation
121provided by Intel is incomplete, and the UDC itself seems to have a
122variety of bugs. The errata for the part is particularly scary! This
123section is an attempt to document some of the discoveries and
124questions I have come across while working on this thing.
125
126pp 11-63 of the "Intel Strong ARM SA-1110 Microprocessor Advanced
127Developer Information" give an ominous warning about how "due to
128internal synchronization required by the UDC configuration registers,
129it is possible for the procesor to write the UDC refisters and FIFOs
130too fast." This has led to a variety of approaches that attempt to
131bang on the hardware repeatedly and read it back until the write
132"sticks."
133
134All of these approaches have been problematic. Currently some macros
135in udc_ctl.h that Nicolas wrote are being used. My hardware guy told
136me that writes would never be "lost" but stuck on some internal bus in
137the UDC module and propagated to the rest of the circuit when the time
138was right. Indeed this seemed to be the case, for example, it seems
139impossible to reliably read back the interrupt mask register of the UDC when in
140the interrupt service routine. Often times the state was not reflected
141on a read until after pending interrupt sources were cleared.
142
143I was feeling prety good about this and was ripping out the looping
144macros right and left until I came upon a situation where, while
145receiving a continuous set of 1 character packets, ep1 (usb_receive.c)
146could not clear receive packet complete (RPC). After much desperate
147faliling about it turns out changing the UDC_flip() macro to bang like
148crazy on the RPC bit did in fact clear it, and clear it
149consistently. So go figure.
150
151Other items of interest:
152
153- Upon emerging from a reset, the UDC will clear the mask register except
154for a mask on suspend.
155
156- USB 1.0 spec says maximum size of a DATA0/1 packet is 64 bytes,
157which is what the character driver is using. However, the UDC can do
158256 bytes and every host I've tried can handle it, even though they
159are not required to. (Perhaps it is a problem when hubs are on the
160line, but the SA UDC has other problems in a hub environment -- like
161even getting the correct address -- per the errata).
162
163- Endpoint zero FIFOs: ARGHHH! Just leave those routines alone.
164Believe me, I have tried every other variation you can think of.
165Probably.
166
167- Sometimes I get a setup request of 0x80 from Windows hosts. I have
168not determined if this is a read_fifo error (none is reported) or if
169this is some undocumented secret Redmond handshake only known to
170initiates of the inner-order.
171
1726. Test Program
173'''''''''''''''
174This is now in the /proc interface. (For good or ill, probably don't
175actually need to dump all this stuff..)
176
1777. Errors and Notes on Intel's 1110 Documentation
178'''''''''''''''''''''''''''''''''''''''''''''''''
179These corrections apply to "Intel StrongARM SA-1110 Microprocessor,
180Advanced Developer's Manual of December 1999" Some of these have been
181corrected in later editions, some not. There have been several updates
182to this document published through 2000. Always use the latest
183available on http://developer.intel.com/design/strong/collateral.htm.
184
185pp 11-65 section 11.8.3.8 bit 2, reserved is now the resume interrupt
186mask. SRM is now SUSIM on SA-1110, and masks only the suspend
187interrupt.
188
189pp 11-67 section 11.8.6, Max IN register, end should be 9 _bytes_
190not 9 bits.
191
192pp 11-68 section 11.8.7.3, SST. This is set by the CPU _not_ the UDC.
193And it looks like you don't get a SST if you FST yourself.
194
195pp 11-68 section 11.8.7.5, DE. This is set by the CPU _not_ the UDC.
196
197pp 11-73 section 11.8.9.7, UDCCS2 table, bit 2, Should be "valid only
198when _TPC_ (not RPC) is set.
199
200pp 11-74 section 11.8.10, should end with a GET_DESCRIPTOR _or
201similar_ command. (Like, for example, GET_CONFIGURATION).
202
203
2048. Change History
205'''''''''''''''''
206Following are current chages  8Mar01 (released in 2.4.2-rmk1-np2?)
207
208- Resetting UDC when coming out of suspend helped enumeration get
209going considerably.
210
211- Added support for client-supplied notify routine to be called
212by the USB core when core reaches "configured" state.
213
214- Added error returns from interrupt reads and buffer flush ioctl
215calls to usb-char. Added usb-char.h file for ioctl calls.
216
217- Fixed bug that kept usb-char transmitter from working the second
218time the module was loaded.
219
220- Turned off a lot of the noise in /proc
221
222- Added specialty routines in ep0 to set and clear bits.
223
224- More enumeration fiddling.
225
226- There are horrible hacks to set max IN length in usb_send
227  that ARE GOING AWAY SOON! REALLY!
228
229*** Following changes 26Feb01 (released in 2.4.2-rmk1-np1)
230
231- usb-eth integration with generic usbnet from AC tree.
232
233- Creation of public interface for usb clients in sa1100_usb.h
234and final separation into a "core" driver (usb_ctl.c, usb_ep0.c
235usb_recv.c usb_send.c) and "client" services (usb-eth.c and
236usb-char.c). Modularized.
237
238- Descriptor handling rewritten. Support for string descriptors
239added. More bugs in ep0 fixed. More setup packets handled.
240
241- /proc interface in usb_ctl returning
242
243- removed client specific stuff from usbd_info_t and hid the
244structure in usb_ctl. Removed RAM-backing of address and pktsize
245in this structure. Now the descriptor values are gospel.
246
247- usb_dbg.h eliminated
248
249- Many bugs fixed in usb-char.c
250
251- Fiddled startup sequence so should start everytime.
252
253- Arch specific "soft connect" hook in usb_ctl.c
254
255- Bumped the interation count in write/set/clear macros
256in usb_ctl.h up to 10000. This seemed to help various bit
257setting in ep0 and usb_send.c.
258
259*** Following changes 10Feb01 release:
260
261- endpoint zero entirely rewitten
262
263- Various changes by Oleg to make Netlink work again after the
264  2.4.1-rmk1-np1 release.
265
266- Resetting of new max packet length done after clearing TPC
267in usb_send, per Nicolas Pitre.
268
269
270*** Following changes 23Jan01 (came out in 2.4.1-rmk1-np1):
271
272- Moved host initiated SET/GET feature stall into endpoint code of
273usb_send.c and usb_receive.c and removed stallep from usb_ctl.c
274Opposite of a SET_FEATURE stall is a reset, so no code to unstall is
275provided.
276
277- Added explicit USB state machine to usb_ctl so driver and device
278state can be tracked closely and explicitly. Added hard-wired
279notification routines in endpoints 1 and 2 so they can track device
280state changes as required. State machine has notion of "zombie" state
281the covers USB states NONATTACHED, ATTACHED and POWERED since these
282are murky, and USB driver currently has no way to differentiate
283between the two.
284
285- Reworked ISR in usb_ctl so reset has higher priority than any other
286event. Stopped using sync macros to clear interrupt pending flags and set
287mask registers since it appears mask register changes are not
288always reflected on a mask register read until the pending flag is
289cleared (yet other tests show they are always cleared
290eventually). Toggle suspend/resume interrupt masks back and forth during
291suspend and resume to debounce and keep UDC internal state machine in
292sync per Intel documentation.
293
294- Flipped UDC flip, clear, write and set macros from do{}while to
295while() loops. Theory is you might save a loop iteration if value
296becomes valid immediately. Also, my hardware guy says writes are never
297"lost", just pipelined and not executed immediately depending on
298internal device conditions (like setting int masks when ints
299pending), so moved write cycles in macros outside of loops.
300
301- Added #defines to SA-1110.h for suspend and resume interrupt mask
302bits per Intel eratta. Submitted to ARM patch system (444/1).
303
304- Removed task queue and defered execution of configure() from
305usb_ctl.
306
307- Removed usb_write_reg() from usb_ctl.c, and various cruft from
308usb_ctl.h.
309
310- Added sa1100_usb_xmitter_avail() to usb_send.c. Makes implementing
311poll() fileop easier.
312
313- Added sa1100_usb_send_reset() to usb_send.c. Makes implementing
314transmitter timeout easier.
315
316- Added API to usb_ctl to set vendor and product ID
317
318- Changed BMATTR descriptor fron int to bulk, when not using netlink. (All
319the docs say UDC does not support INT xfers -- though, at the protocol
320level I don't see why not, since bulk and int are both just
321IN-DATA-ACK. I figure netlink may rely on this, and not just a
322continuous pending read from the host, but for "pure bulk" host
323polling may not be generally correct.)
324
325- Removed unused rx_lock and tx_lock from usb_ctl
326
327- Converted everyone to SA-1100.h and nuked hardware defines in usb_ctl.h
328
329- Removed udc_init() in usb_ctl.c and folded functionality into udc_start().
330
331- Clear force stall (FST) in udc_start and reset so UDC actually runs when
332first turned on.
333
334- Emit NAK in receiver until ep1_start() for error (RPE) case too.
335
336- Remove enable/disable UDC from reset handler in udc_ctl. The UDC has
337already been reset, so no need to do this again.
338
339- Explicitly set address to zero in ep0_reset()
340
341- Added "naking" boolean to usb_receiv.c. An attempt to solve a
342hypothetical race condition where we are in the critical section
343initiating a read from base-level code, a RPC happens, and start()
344might clear the condition before the packet is handled by the ISR.
345