summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaurus Cuelenaere <mcuelenaere@gmail.com>2010-05-03 20:36:34 +0000
committerMaurus Cuelenaere <mcuelenaere@gmail.com>2010-05-03 20:36:34 +0000
commit056f608f7832e2aea633cb89be4b5b53cb6ebe5a (patch)
treedd1052c8c5d341e77e94055b4fed23ba2468d1f6
parent35f341464b7d9dc2b1bd3f6222da5ec226f34dad (diff)
downloadrockbox-056f608f7832e2aea633cb89be4b5b53cb6ebe5a.tar.gz
rockbox-056f608f7832e2aea633cb89be4b5b53cb6ebe5a.zip
Ingenic Jz4740: eliminate EP0 state machine + small cleanups
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@25797 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/target/mips/ingenic_jz47xx/usb-jz4740.c99
1 files changed, 33 insertions, 66 deletions
diff --git a/firmware/target/mips/ingenic_jz47xx/usb-jz4740.c b/firmware/target/mips/ingenic_jz47xx/usb-jz4740.c
index 81c6bdb1ed..f12f1bed82 100644
--- a/firmware/target/mips/ingenic_jz47xx/usb-jz4740.c
+++ b/firmware/target/mips/ingenic_jz47xx/usb-jz4740.c
@@ -36,13 +36,6 @@
36 up to 15 endpoints (the Jz4740 only has 5). 36 up to 15 endpoints (the Jz4740 only has 5).
37*/ 37*/
38 38
39enum
40{
41 USB_EP0_IDLE,
42 USB_EP0_RX,
43 USB_EP0_TX
44};
45
46#define EP_BUF_LEFT(ep) ((ep)->length - (ep)->sent) 39#define EP_BUF_LEFT(ep) ((ep)->length - (ep)->sent)
47#define EP_PTR(ep) ((void*)((unsigned int)(ep)->buf + (ep)->sent)) 40#define EP_PTR(ep) ((void*)((unsigned int)(ep)->buf + (ep)->sent))
48#define EP_NUMBER(ep) (((int)(ep) - (int)&endpoints[0])/sizeof(struct usb_endpoint)) 41#define EP_NUMBER(ep) (((int)(ep) - (int)&endpoints[0])/sizeof(struct usb_endpoint))
@@ -61,30 +54,29 @@ enum ep_type
61struct usb_endpoint 54struct usb_endpoint
62{ 55{
63 void *buf; 56 void *buf;
64 unsigned int length; 57 size_t length;
65 union 58 union
66 { 59 {
67 unsigned int sent; 60 size_t sent;
68 unsigned int received; 61 size_t received;
69 }; 62 };
70 bool busy; 63 bool busy;
71 64
72 const enum ep_type type; 65 const enum ep_type type;
73 const bool use_dma; 66 const bool use_dma;
74 bool wait;
75 67
76 const unsigned int fifo_addr; 68 const long fifo_addr;
77 unsigned short fifo_size; 69 unsigned short fifo_size;
78 70
71 bool wait;
79 struct wakeup wakeup; 72 struct wakeup wakeup;
80}; 73};
81 74
82static unsigned char ep0_rx_buf[64]; 75static unsigned char ep0_rx_buf[64];
83static unsigned char ep0state = USB_EP0_IDLE;
84static struct usb_endpoint endpoints[] = 76static struct usb_endpoint endpoints[] =
85{ 77{
86 { .type = ep_control, .fifo_addr = USB_FIFO_EP0, .fifo_size = 64 }, 78 { .type = ep_control, .fifo_addr = USB_FIFO_EP0, .fifo_size = 64 },
87 { .type = ep_control, .fifo_addr = USB_FIFO_EP0, .fifo_size = 64, .buf = &ep0_rx_buf }, 79 { .type = ep_control, .fifo_addr = USB_FIFO_EP0, .buf = &ep0_rx_buf },
88 { .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 }, 80 { .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 },
89 { .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 }, 81 { .type = ep_bulk, .fifo_addr = USB_FIFO_EP1, .fifo_size = 512 },
90 { .type = ep_interrupt, .fifo_addr = USB_FIFO_EP2, .fifo_size = 64 }, 82 { .type = ep_interrupt, .fifo_addr = USB_FIFO_EP2, .fifo_size = 64 },
@@ -172,6 +164,16 @@ static void flushFIFO(struct usb_endpoint *ep)
172 } 164 }
173} 165}
174 166
167static inline void ep_transfer_completed(struct usb_endpoint* ep)
168{
169 ep->sent = 0;
170 ep->length = 0;
171 ep->buf = NULL;
172 ep->busy = false;
173 if(ep->wait)
174 wakeup_signal(&ep->wakeup);
175}
176
175static void EP0_send(void) 177static void EP0_send(void)
176{ 178{
177 struct usb_endpoint* ep = &endpoints[0]; 179 struct usb_endpoint* ep = &endpoints[0];
@@ -181,13 +183,6 @@ static void EP0_send(void)
181 select_endpoint(0); 183 select_endpoint(0);
182 csr0 = REG_USB_REG_CSR0; 184 csr0 = REG_USB_REG_CSR0;
183 185
184 if(ep->length == 0)
185 {
186 //REG_USB_REG_CSR0 = (csr0 | USB_CSR0_SVDOUTPKTRDY | USB_CSR0_DATAEND);
187 REG_USB_REG_CSR0 = (csr0 | USB_CSR0_SVDOUTPKTRDY);
188 return;
189 }
190
191 if(ep->sent == 0) 186 if(ep->sent == 0)
192 length = MIN(ep->length, ep->fifo_size); 187 length = MIN(ep->length, ep->fifo_size);
193 else 188 else
@@ -199,9 +194,8 @@ static void EP0_send(void)
199 if(ep->sent >= ep->length) 194 if(ep->sent >= ep->length)
200 { 195 {
201 REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY | USB_CSR0_DATAEND); /* Set data end! */ 196 REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY | USB_CSR0_DATAEND); /* Set data end! */
202 ep0state = USB_EP0_IDLE; 197 usb_core_transfer_complete(0, USB_DIR_IN, 0, ep->sent);
203 if(ep->wait) 198 ep_transfer_completed(ep);
204 wakeup_signal(&ep->wakeup);
205 } 199 }
206 else 200 else
207 REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY); 201 REG_USB_REG_CSR0 = (csr0 | USB_CSR0_INPKTRDY);
@@ -212,6 +206,8 @@ static void EP0_handler(void)
212 logf("%s()", __func__); 206 logf("%s()", __func__);
213 207
214 unsigned char csr0; 208 unsigned char csr0;
209 struct usb_endpoint *ep_send = &endpoints[0];
210 struct usb_endpoint *ep_recv = &endpoints[1];
215 211
216 /* Read CSR0 */ 212 /* Read CSR0 */
217 select_endpoint(0); 213 select_endpoint(0);
@@ -223,7 +219,6 @@ static void EP0_handler(void)
223 if(csr0 & USB_CSR0_SENTSTALL) 219 if(csr0 & USB_CSR0_SENTSTALL)
224 { 220 {
225 REG_USB_REG_CSR0 = csr0 & ~USB_CSR0_SENTSTALL; 221 REG_USB_REG_CSR0 = csr0 & ~USB_CSR0_SENTSTALL;
226 ep0state = USB_EP0_IDLE;
227 return; 222 return;
228 } 223 }
229 224
@@ -235,24 +230,18 @@ static void EP0_handler(void)
235 if(csr0 & USB_CSR0_SETUPEND) 230 if(csr0 & USB_CSR0_SETUPEND)
236 { 231 {
237 REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDSETUPEND; 232 REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDSETUPEND;
238 ep0state = USB_EP0_IDLE;
239 return; 233 return;
240 } 234 }
241 235
242 /* Call relevant routines for endpoint 0 state */ 236 /* Call relevant routines for endpoint 0 state */
243 if(ep0state == USB_EP0_IDLE) 237 if(ep_send->busy)
238 EP0_send();
239 else if(csr0 & USB_CSR0_OUTPKTRDY) /* There is a packet in the fifo */
244 { 240 {
245 if(csr0 & USB_CSR0_OUTPKTRDY) /* There is a packet in the fifo */ 241 readFIFO(ep_recv, REG_USB_REG_COUNT0);
246 { 242 REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY; /* clear OUTPKTRDY bit */
247 readFIFO(&endpoints[1], REG_USB_REG_COUNT0); 243 usb_core_control_request((struct usb_ctrlrequest*)ep_recv->buf);
248 REG_USB_REG_CSR0 = csr0 | USB_CSR0_SVDOUTPKTRDY; /* clear OUTPKTRDY bit */
249 usb_core_control_request((struct usb_ctrlrequest*)endpoints[1].buf);
250 if(endpoints[1].wait)
251 wakeup_signal(&endpoints[1].wakeup);
252 }
253 } 244 }
254 else if(ep0state == USB_EP0_TX)
255 EP0_send();
256} 245}
257 246
258static void EPIN_handler(unsigned int endpoint) 247static void EPIN_handler(unsigned int endpoint)
@@ -299,13 +288,8 @@ static void EPIN_handler(unsigned int endpoint)
299 if(ep->sent >= ep->length) 288 if(ep->sent >= ep->length)
300 { 289 {
301 usb_core_transfer_complete(endpoint, USB_DIR_IN, 0, ep->sent); 290 usb_core_transfer_complete(endpoint, USB_DIR_IN, 0, ep->sent);
302 if(ep->wait) 291 ep_transfer_completed(ep);
303 wakeup_signal(&ep->wakeup);
304 logf("sent complete"); 292 logf("sent complete");
305 ep->sent = 0;
306 ep->length = 0;
307 ep->buf = NULL;
308 ep->busy = false;
309 } 293 }
310} 294}
311 295
@@ -352,13 +336,8 @@ static void EPOUT_handler(unsigned int endpoint)
352 if(size < ep->fifo_size || ep->received >= ep->length) 336 if(size < ep->fifo_size || ep->received >= ep->length)
353 { 337 {
354 usb_core_transfer_complete(endpoint, USB_DIR_OUT, 0, ep->received); 338 usb_core_transfer_complete(endpoint, USB_DIR_OUT, 0, ep->received);
355 if(ep->wait) 339 ep_transfer_completed(ep);
356 wakeup_signal(&ep->wakeup);
357 logf("receive transfer_complete"); 340 logf("receive transfer_complete");
358 ep->received = 0;
359 ep->length = 0;
360 ep->buf = NULL;
361 ep->busy = false;
362 } 341 }
363 } 342 }
364 } 343 }
@@ -407,13 +386,7 @@ static void EPDMA_handler(int number)
407 386
408 usb_core_transfer_complete(endpoint, EP_IS_IN(ep) ? USB_DIR_IN : USB_DIR_OUT, 387 usb_core_transfer_complete(endpoint, EP_IS_IN(ep) ? USB_DIR_IN : USB_DIR_OUT,
409 0, ep->length); 388 0, ep->length);
410 389 ep_transfer_completed(ep);
411 ep->busy = false;
412 ep->sent = 0;
413 ep->length = 0;
414 ep->buf = NULL;
415 if(ep->wait)
416 wakeup_signal(&ep->wakeup);
417} 390}
418 391
419static void setup_endpoint(struct usb_endpoint *ep) 392static void setup_endpoint(struct usb_endpoint *ep)
@@ -486,9 +459,6 @@ static void udc_reset(void)
486 459
487 unsigned int i; 460 unsigned int i;
488 461
489 /* EP0 init */
490 ep0state = USB_EP0_IDLE;
491
492 /* Disable interrupts */ 462 /* Disable interrupts */
493 REG_USB_REG_INTRINE = 0; 463 REG_USB_REG_INTRINE = 0;
494 REG_USB_REG_INTROUTE = 0; 464 REG_USB_REG_INTROUTE = 0;
@@ -710,7 +680,7 @@ void usb_drv_set_address(int address)
710static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length, bool blocking) 680static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length, bool blocking)
711{ 681{
712 if(ep->type == ep_control && ptr == NULL && length == 0) 682 if(ep->type == ep_control && ptr == NULL && length == 0)
713 return; /* ACK request, handled by the USB controller */ 683 return; /* ACK request, handled in the ISR */
714 684
715 int flags = disable_irq_save(); 685 int flags = disable_irq_save();
716 686
@@ -723,7 +693,6 @@ static void usb_drv_send_internal(struct usb_endpoint* ep, void* ptr, int length
723 693
724 if(ep->type == ep_control) 694 if(ep->type == ep_control)
725 { 695 {
726 ep0state = USB_EP0_TX;
727 EP0_send(); 696 EP0_send();
728 } 697 }
729 else 698 else
@@ -777,8 +746,8 @@ int usb_drv_recv(int endpoint, void* ptr, int length)
777 746
778 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length); 747 logf("%s(%d, 0x%x, %d)", __func__, endpoint, (int)ptr, length);
779 748
780 if(endpoint == EP_CONTROL && ptr == NULL && length == 0) 749 if(endpoint == EP_CONTROL)
781 return 0; /* ACK request, handled by the USB controller */ 750 return 0; /* all EP0 OUT transactions are handled within the ISR */
782 else 751 else
783 { 752 {
784 flags = disable_irq_save(); 753 flags = disable_irq_save();
@@ -844,7 +813,7 @@ void usb_drv_cancel_all_transfers(void)
844 813
845 for(i=0; i<TOTAL_EP(); i++) 814 for(i=0; i<TOTAL_EP(); i++)
846 { 815 {
847 if(i >= 2) 816 if(i != 1) /* ep0 out needs special handling */
848 endpoints[i].buf = NULL; 817 endpoints[i].buf = NULL;
849 818
850 endpoints[i].sent = 0; 819 endpoints[i].sent = 0;
@@ -854,8 +823,6 @@ void usb_drv_cancel_all_transfers(void)
854 flushFIFO(&endpoints[i]); 823 flushFIFO(&endpoints[i]);
855 } 824 }
856 restore_irq(flags); 825 restore_irq(flags);
857
858 ep0state = USB_EP0_IDLE;
859} 826}
860 827
861void usb_drv_release_endpoint(int ep) 828void usb_drv_release_endpoint(int ep)