1 /*****************************************************************************
2 ** FILE NAME : ifxusb_cif.h
3 ** PROJECT : IFX USB sub-system V3
4 ** MODULES : IFX USB sub-system Host and Device driver
7 ** AUTHOR : Chen, Howard
8 ** DESCRIPTION : The Core Interface provides basic services for accessing and
9 ** managing the IFX USB hardware. These services are used by both the
10 ** Host Controller Driver and the Peripheral Controller Driver.
13 ** REFERENCE : IFX hardware ref handbook for each plateforms
15 ** Version Control Section **
19 ** $Log$ Revision history
20 *****************************************************************************/
23 \defgroup IFXUSB_DRIVER_V3 IFX USB SS Project
24 \brief IFX USB subsystem V3.x
28 \defgroup IFXUSB_CIF Core Interface APIs
29 \ingroup IFXUSB_DRIVER_V3
30 \brief The Core Interface provides basic services for accessing and
31 managing the IFXUSB hardware. These services are used by both the
32 Host Controller Driver and the Peripheral Controller Driver.
38 \ingroup IFXUSB_DRIVER_V3
39 \brief This file contains the interface to the IFX USB Core.
42 #if !defined(__IFXUSB_CIF_H__)
43 #define __IFXUSB_CIF_H__
45 #include <linux/workqueue.h>
47 #include <linux/version.h>
48 #include <asm/param.h>
50 #include "ifxusb_plat.h"
51 #include "ifxusb_regs.h"
54 #include "linux/timer.h"
57 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
59 #define IFXUSB_PARAM_SPEED_HIGH 0
60 #define IFXUSB_PARAM_SPEED_FULL 1
62 #define IFXUSB_EP_SPEED_LOW 0
63 #define IFXUSB_EP_SPEED_FULL 1
64 #define IFXUSB_EP_SPEED_HIGH 2
66 #define IFXUSB_EP_TYPE_CTRL 0
67 #define IFXUSB_EP_TYPE_ISOC 1
68 #define IFXUSB_EP_TYPE_BULK 2
69 #define IFXUSB_EP_TYPE_INTR 3
71 #define IFXUSB_HC_PID_DATA0 0
72 #define IFXUSB_HC_PID_DATA2 1
73 #define IFXUSB_HC_PID_DATA1 2
74 #define IFXUSB_HC_PID_MDATA 3
75 #define IFXUSB_HC_PID_SETUP 3
79 \addtogroup IFXUSB_CIF
85 \brief IFXUSB Parameters structure.
86 This structure is used for both importing from insmod stage and run-time storage.
87 These parameters define how the IFXUSB controller should be configured.
89 typedef struct ifxusb_params
91 int32_t dma_burst_size; /*!< The DMA Burst size (applicable only for Internal DMA
92 Mode). 0(for single), 1(incr), 4(incr4), 8(incr8) 16(incr16)
94 /* Translate this to GAHBCFG values */
95 int32_t speed; /*!< Specifies the maximum speed of operation in host and device mode.
96 The actual speed depends on the speed of the attached device and
97 the value of phy_type. The actual speed depends on the speed of the
99 0 - High Speed (default)
103 int32_t data_fifo_size; /*!< Total number of dwords in the data FIFO memory. This
104 memory includes the Rx FIFO, non-periodic Tx FIFO, and periodic
109 int32_t rx_fifo_size; /*!< Number of dwords in the Rx FIFO in device mode.
114 int32_t tx_fifo_size[MAX_EPS_CHANNELS]; /*!< Number of dwords in each of the Tx FIFOs in device mode.
118 int32_t thr_ctl; /*!< Threshold control on/off */
119 int32_t tx_thr_length; /*!< Threshold length for Tx */
120 int32_t rx_thr_length; /*!< Threshold length for Rx*/
123 int32_t host_channels; /*!< The number of host channel registers to use.
127 int32_t rx_fifo_size; /*!< Number of dwords in the Rx FIFO in host mode.
131 int32_t nperio_tx_fifo_size;/*!< Number of dwords in the non-periodic Tx FIFO in host mode.
135 int32_t perio_tx_fifo_size; /*!< Number of dwords in the host periodic Tx FIFO.
140 int32_t max_transfer_size; /*!< The maximum transfer size supported in bytes.
144 int32_t max_packet_count; /*!< The maximum number of packets in a transfer.
145 15 to 511 (default 511)
147 int32_t phy_utmi_width; /*!< Specifies the UTMI+ Data Width.
148 8 or 16 bits (default 16)
151 int32_t turn_around_time_hs; /*!< Specifies the Turn-Around time at HS*/
152 int32_t turn_around_time_fs; /*!< Specifies the Turn-Around time at FS*/
154 int32_t timeout_cal_hs; /*!< Specifies the Timeout_Calibration at HS*/
155 int32_t timeout_cal_fs; /*!< Specifies the Timeout_Calibration at FS*/
158 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
162 \struct ifxusb_core_if
163 \brief The ifx_core_if structure contains information needed to manage
164 the IFX USB controller acting in either host or device mode. It
165 represents the programming view of the controller as a whole.
167 typedef struct ifxusb_core_if
169 ifxusb_params_t params; /*!< Run-time Parameters */
171 uint8_t core_no; /*!< core number (used as id when multi-core case */
172 char *core_name; /*!< core name used for registration and informative purpose*/
173 int irq; /*!< irq number this core is hooked */
175 /*****************************************************************
176 * Structures and pointers to physical register interface.
177 *****************************************************************/
178 /** Core Global registers starting at offset 000h. */
179 ifxusb_core_global_regs_t *core_global_regs; /*!< pointer to Core Global Registers, offset at 000h */
181 /** Host-specific registers */
183 /** Host Global Registers starting at offset 400h.*/
184 ifxusb_host_global_regs_t *host_global_regs; /*!< pointer to Host Global Registers, offset at 400h */
185 #define IFXUSB_HOST_GLOBAL_REG_OFFSET 0x400
186 /** Host Port 0 Control and Status Register */
187 volatile uint32_t *hprt0; /*!< pointer to HPRT0 Registers, offset at 440h */
188 #define IFXUSB_HOST_PORT_REGS_OFFSET 0x440
189 /** Host Channel Specific Registers at offsets 500h-5FCh. */
190 ifxusb_hc_regs_t *hc_regs[MAX_EPS_CHANNELS]; /*!< pointer to Host-Channel n Registers, offset at 500h */
191 #define IFXUSB_HOST_CHAN_REGS_OFFSET 0x500
192 #define IFXUSB_CHAN_REGS_OFFSET 0x20
195 /** Device-specific registers */
197 /** Device Global Registers starting at offset 800h */
198 ifxusb_device_global_regs_t *dev_global_regs; /*!< pointer to Device Global Registers, offset at 800h */
199 #define IFXUSB_DEV_GLOBAL_REG_OFFSET 0x800
201 /** Device Logical IN Endpoint-Specific Registers 900h-AFCh */
202 ifxusb_dev_in_ep_regs_t *in_ep_regs[MAX_EPS_CHANNELS]; /*!< pointer to Device IN-EP Registers, offset at 900h */
203 #define IFXUSB_DEV_IN_EP_REG_OFFSET 0x900
204 #define IFXUSB_EP_REG_OFFSET 0x20
205 /** Device Logical OUT Endpoint-Specific Registers B00h-CFCh */
206 ifxusb_dev_out_ep_regs_t *out_ep_regs[MAX_EPS_CHANNELS];/*!< pointer to Device OUT-EP Registers, offset at 900h */
207 #define IFXUSB_DEV_OUT_EP_REG_OFFSET 0xB00
210 /** Power and Clock Gating Control Register */
211 volatile uint32_t *pcgcctl; /*!< pointer to Power and Clock Gating Control Registers, offset at E00h */
212 #define IFXUSB_PCGCCTL_OFFSET 0xE00
214 /** Push/pop addresses for endpoints or host channels.*/
215 uint32_t *data_fifo[MAX_EPS_CHANNELS]; /*!< pointer to FIFO access windows, offset at 1000h */
216 #define IFXUSB_DATA_FIFO_OFFSET 0x1000
217 #define IFXUSB_DATA_FIFO_SIZE 0x1000
219 uint32_t *data_fifo_dbg; /*!< pointer to FIFO debug windows, offset at 1000h */
221 /** Hardware Configuration -- stored here for convenience.*/
222 hwcfg1_data_t hwcfg1; /*!< preserved Hardware Configuration 1 */
223 hwcfg2_data_t hwcfg2; /*!< preserved Hardware Configuration 2 */
224 hwcfg3_data_t hwcfg3; /*!< preserved Hardware Configuration 3 */
225 hwcfg4_data_t hwcfg4; /*!< preserved Hardware Configuration 3 */
226 uint32_t snpsid; /*!< preserved SNPSID */
228 /*****************************************************************
229 * Run-time informations.
230 *****************************************************************/
231 /* Set to 1 if the core PHY interface bits in USBCFG have been initialized. */
232 uint8_t phy_init_done; /*!< indicated PHY is initialized. */
235 uint8_t queuing_high_bandwidth; /*!< Host mode, Queueing High Bandwidth. */
243 \fn void *ifxusb_alloc_buf(size_t size, int clear)
244 \brief This function is called to allocate buffer of specified size.
245 The allocated buffer is mapped into DMA accessable address.
246 \param size Size in BYTE to be allocated
247 \param clear 0: don't do clear after buffer allocated, other: do clear to zero
248 \return 0/NULL: Fail; uncached pointer of allocated buffer
251 extern void *ifxusb_alloc_buf(size_t size, int clear);
254 \fn void ifxusb_free_buf(void *vaddr)
255 \brief This function is called to free allocated buffer.
256 \param vaddr the uncached pointer of the buffer
259 extern void ifxusb_free_buf(void *vaddr);
262 \fn int ifxusb_core_if_init(ifxusb_core_if_t *_core_if,
264 uint32_t _reg_base_addr,
265 uint32_t _fifo_base_addr,
266 uint32_t _fifo_dbg_addr)
267 \brief This function is called to initialize the IFXUSB CSR data
268 structures. The register addresses in the device and host
269 structures are initialized from the base address supplied by the
270 caller. The calling function must make the OS calls to get the
271 base address of the IFXUSB controller registers.
272 \param _core_if Pointer of core_if structure
273 \param _irq irq number
274 \param _reg_base_addr Base address of IFXUSB core registers
275 \param _fifo_base_addr Fifo base address
276 \param _fifo_dbg_addr Fifo debug address
280 extern int ifxusb_core_if_init(ifxusb_core_if_t *_core_if,
282 uint32_t _reg_base_addr,
283 uint32_t _fifo_base_addr,
284 uint32_t _fifo_dbg_addr);
288 \fn void ifxusb_core_if_remove(ifxusb_core_if_t *_core_if)
289 \brief This function free the mapped address in the IFXUSB CSR data structures.
290 \param _core_if Pointer of core_if structure
293 extern void ifxusb_core_if_remove(ifxusb_core_if_t *_core_if);
296 \fn void ifxusb_enable_global_interrupts( ifxusb_core_if_t *_core_if )
297 \brief This function enbles the controller's Global Interrupt in the AHB Config register.
298 \param _core_if Pointer of core_if structure
300 extern void ifxusb_enable_global_interrupts( ifxusb_core_if_t *_core_if );
303 \fn void ifxusb_disable_global_interrupts( ifxusb_core_if_t *_core_if )
304 \brief This function disables the controller's Global Interrupt in the AHB Config register.
305 \param _core_if Pointer of core_if structure
308 extern void ifxusb_disable_global_interrupts( ifxusb_core_if_t *_core_if );
311 \fn void ifxusb_flush_tx_fifo( ifxusb_core_if_t *_core_if, const int _num )
312 \brief Flush a Tx FIFO.
313 \param _core_if Pointer of core_if structure
314 \param _num Tx FIFO to flush. ( 0x10 for ALL TX FIFO )
317 extern void ifxusb_flush_tx_fifo( ifxusb_core_if_t *_core_if, const int _num );
320 \fn void ifxusb_flush_rx_fifo( ifxusb_core_if_t *_core_if )
321 \brief Flush Rx FIFO.
322 \param _core_if Pointer of core_if structure
325 extern void ifxusb_flush_rx_fifo( ifxusb_core_if_t *_core_if );
328 \fn void ifxusb_flush_both_fifo( ifxusb_core_if_t *_core_if )
329 \brief Flush ALL Rx and Tx FIFO.
330 \param _core_if Pointer of core_if structure
333 extern void ifxusb_flush_both_fifo( ifxusb_core_if_t *_core_if );
337 \fn int ifxusb_core_soft_reset(ifxusb_core_if_t *_core_if)
338 \brief Do core a soft reset of the core. Be careful with this because it
339 resets all the internal state machines of the core.
340 \param _core_if Pointer of core_if structure
343 extern int ifxusb_core_soft_reset(ifxusb_core_if_t *_core_if);
347 \brief Turn on the USB Core Power
348 \param _core_if Pointer of core_if structure
351 extern void ifxusb_power_on (ifxusb_core_if_t *_core_if);
354 \fn void ifxusb_power_off (ifxusb_core_if_t *_core_if)
355 \brief Turn off the USB Core Power
356 \param _core_if Pointer of core_if structure
359 extern void ifxusb_power_off (ifxusb_core_if_t *_core_if);
362 \fn void ifxusb_phy_power_on (ifxusb_core_if_t *_core_if)
363 \brief Turn on the USB PHY Power
364 \param _core_if Pointer of core_if structure
367 extern void ifxusb_phy_power_on (ifxusb_core_if_t *_core_if);
370 \fn void ifxusb_phy_power_off (ifxusb_core_if_t *_core_if)
371 \brief Turn off the USB PHY Power
372 \param _core_if Pointer of core_if structure
375 extern void ifxusb_phy_power_off (ifxusb_core_if_t *_core_if);
378 \fn void ifxusb_hard_reset(ifxusb_core_if_t *_core_if)
379 \brief Reset on the USB Core RCU
380 \param _core_if Pointer of core_if structure
383 extern void ifxusb_hard_reset(ifxusb_core_if_t *_core_if);
385 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
390 \fn void ifxusb_host_core_init(ifxusb_core_if_t *_core_if, ifxusb_params_t *_params)
391 \brief This function initializes the IFXUSB controller registers for Host mode.
392 This function flushes the Tx and Rx FIFOs and it flushes any entries in the
394 \param _core_if Pointer of core_if structure
395 \param _params parameters to be set
398 extern void ifxusb_host_core_init(ifxusb_core_if_t *_core_if, ifxusb_params_t *_params);
401 \fn void ifxusb_host_enable_interrupts(ifxusb_core_if_t *_core_if)
402 \brief This function enables the Host mode interrupts.
403 \param _core_if Pointer of core_if structure
406 extern void ifxusb_host_enable_interrupts(ifxusb_core_if_t *_core_if);
409 \fn void ifxusb_host_disable_interrupts(ifxusb_core_if_t *_core_if)
410 \brief This function disables the Host mode interrupts.
411 \param _core_if Pointer of core_if structure
414 extern void ifxusb_host_disable_interrupts(ifxusb_core_if_t *_core_if);
416 #if defined(__IS_TWINPASS__)
417 extern void ifxusb_enable_afe_oc(void);
421 \fn void ifxusb_vbus_init(ifxusb_core_if_t *_core_if)
422 \brief This function init the VBUS control.
423 \param _core_if Pointer of core_if structure
426 extern void ifxusb_vbus_init(ifxusb_core_if_t *_core_if);
429 \fn void ifxusb_vbus_free(ifxusb_core_if_t *_core_if)
430 \brief This function free the VBUS control.
431 \param _core_if Pointer of core_if structure
434 extern void ifxusb_vbus_free(ifxusb_core_if_t *_core_if);
437 \fn void ifxusb_vbus_on(ifxusb_core_if_t *_core_if)
438 \brief Turn on the USB 5V VBus Power
439 \param _core_if Pointer of core_if structure
442 extern void ifxusb_vbus_on(ifxusb_core_if_t *_core_if);
445 \fn void ifxusb_vbus_off(ifxusb_core_if_t *_core_if)
446 \brief Turn off the USB 5V VBus Power
447 \param _core_if Pointer of core_if structure
450 extern void ifxusb_vbus_off(ifxusb_core_if_t *_core_if);
453 \fn int ifxusb_vbus(ifxusb_core_if_t *_core_if)
454 \brief Read Current VBus status
455 \param _core_if Pointer of core_if structure
458 extern int ifxusb_vbus(ifxusb_core_if_t *_core_if);
460 #if defined(__DO_OC_INT__) && defined(__DO_OC_INT_ENABLE__)
462 \fn void ifxusb_oc_int_on(void)
463 \brief Turn on the OC interrupt
466 extern void ifxusb_oc_int_on(void);
469 \fn void ifxusb_oc_int_off(void)
470 \brief Turn off the OC interrupt
473 extern void ifxusb_oc_int_off(void);
474 #endif //defined(__DO_OC_INT__) && defined(__DO_OC_INT_ENABLE__)
477 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
482 \fn void ifxusb_dev_enable_interrupts(ifxusb_core_if_t *_core_if)
483 \brief This function enables the Device mode interrupts.
484 \param _core_if Pointer of core_if structure
487 extern void ifxusb_dev_enable_interrupts(ifxusb_core_if_t *_core_if);
490 \fn uint32_t ifxusb_dev_get_frame_number(ifxusb_core_if_t *_core_if)
491 \brief Gets the current USB frame number. This is the frame number from the last SOF packet.
492 \param _core_if Pointer of core_if structure
495 extern uint32_t ifxusb_dev_get_frame_number(ifxusb_core_if_t *_core_if);
498 \fn void ifxusb_dev_ep_set_stall(ifxusb_core_if_t *_core_if, uint8_t _epno, uint8_t _is_in)
499 \brief Set the EP STALL.
500 \param _core_if Pointer of core_if structure
501 \param _epno EP number
502 \param _is_in 1: is IN transfer
505 extern void ifxusb_dev_ep_set_stall(ifxusb_core_if_t *_core_if, uint8_t _epno, uint8_t _is_in);
508 \fn void ifxusb_dev_ep_clear_stall(ifxusb_core_if_t *_core_if, uint8_t _epno, uint8_t _ep_type, uint8_t _is_in)
509 \brief Set the EP STALL.
510 \param _core_if Pointer of core_if structure
511 \param _epno EP number
512 \param _ep_type EP Type
515 extern void ifxusb_dev_ep_clear_stall(ifxusb_core_if_t *_core_if, uint8_t _epno, uint8_t _ep_type, uint8_t _is_in);
518 \fn void ifxusb_dev_core_init(ifxusb_core_if_t *_core_if, ifxusb_params_t *_params)
519 \brief This function initializes the IFXUSB controller registers for Device mode.
520 This function flushes the Tx and Rx FIFOs and it flushes any entries in the
522 This function validate the imported parameters and store the result in the CIF structure.
524 \param _core_if Pointer of core_if structure
525 \param _params structure of inported parameters
528 extern void ifxusb_dev_core_init(ifxusb_core_if_t *_core_if, ifxusb_params_t *_params);
531 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
533 #if defined(__GADGET_LED__) || defined(__HOST_LED__)
535 \fn void ifxusb_led_init(ifxusb_core_if_t *_core_if)
536 \brief This function init the LED control.
537 \param _core_if Pointer of core_if structure
540 extern void ifxusb_led_init(ifxusb_core_if_t *_core_if);
543 \fn void ifxusb_led_free(ifxusb_core_if_t *_core_if)
544 \brief This function free the LED control.
545 \param _core_if Pointer of core_if structure
548 extern void ifxusb_led_free(ifxusb_core_if_t *_core_if);
551 \fn void ifxusb_led(ifxusb_core_if_t *_core_if)
552 \brief This function trigger the LED access.
553 \param _core_if Pointer of core_if structure
556 extern void ifxusb_led(ifxusb_core_if_t *_core_if);
559 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
561 /* internal routines for debugging */
562 extern void ifxusb_dump_msg(const u8 *buf, unsigned int length);
563 extern void ifxusb_dump_spram(ifxusb_core_if_t *_core_if);
564 extern void ifxusb_dump_registers(ifxusb_core_if_t *_core_if);
565 extern void ifxusb_clean_spram(ifxusb_core_if_t *_core_if,uint32_t dwords);
567 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
569 static inline uint32_t ifxusb_read_core_intr(ifxusb_core_if_t *_core_if)
571 return (ifxusb_rreg(&_core_if->core_global_regs->gintsts) &
572 (ifxusb_rreg(&_core_if->core_global_regs->gintmsk)
573 #ifdef __USE_TIMER_4_SOF__
574 | IFXUSB_SOF_INTR_MASK
579 static inline uint32_t ifxusb_read_otg_intr (ifxusb_core_if_t *_core_if)
581 return (ifxusb_rreg (&_core_if->core_global_regs->gotgint));
584 static inline uint32_t ifxusb_mode(ifxusb_core_if_t *_core_if)
586 return (ifxusb_rreg( &_core_if->core_global_regs->gintsts ) & 0x1);
588 static inline uint8_t ifxusb_is_device_mode(ifxusb_core_if_t *_core_if)
590 return (ifxusb_mode(_core_if) != 1);
592 static inline uint8_t ifxusb_is_host_mode(ifxusb_core_if_t *_core_if)
594 return (ifxusb_mode(_core_if) == 1);
597 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
600 static inline uint32_t ifxusb_read_hprt0(ifxusb_core_if_t *_core_if)
603 hprt0.d32 = ifxusb_rreg(_core_if->hprt0);
605 hprt0.b.prtconndet = 0;
606 hprt0.b.prtenchng = 0;
607 hprt0.b.prtovrcurrchng = 0;
611 static inline uint32_t ifxusb_read_host_all_channels_intr (ifxusb_core_if_t *_core_if)
613 return (ifxusb_rreg (&_core_if->host_global_regs->haint));
616 static inline uint32_t ifxusb_read_host_channel_intr (ifxusb_core_if_t *_core_if, int hc_num)
618 return (ifxusb_rreg (&_core_if->hc_regs[hc_num]->hcint));
623 static inline uint32_t ifxusb_read_dev_all_in_ep_intr(ifxusb_core_if_t *_core_if)
626 v = ifxusb_rreg(&_core_if->dev_global_regs->daint) &
627 ifxusb_rreg(&_core_if->dev_global_regs->daintmsk);
631 static inline uint32_t ifxusb_read_dev_all_out_ep_intr(ifxusb_core_if_t *_core_if)
634 v = ifxusb_rreg(&_core_if->dev_global_regs->daint) &
635 ifxusb_rreg(&_core_if->dev_global_regs->daintmsk);
636 return ((v & 0xffff0000) >> 16);
639 static inline uint32_t ifxusb_read_dev_in_ep_intr(ifxusb_core_if_t *_core_if, int _ep_num)
642 v = ifxusb_rreg(&_core_if->in_ep_regs[_ep_num]->diepint) &
643 ifxusb_rreg(&_core_if->dev_global_regs->diepmsk);
647 static inline uint32_t ifxusb_read_dev_out_ep_intr(ifxusb_core_if_t *_core_if, int _ep_num)
650 v = ifxusb_rreg(&_core_if->out_ep_regs[_ep_num]->doepint) &
651 ifxusb_rreg(&_core_if->dev_global_regs->doepmsk);
657 extern void ifxusb_attr_create (void *_dev);
659 extern void ifxusb_attr_remove (void *_dev);
661 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
663 #endif // !defined(__IFXUSB_CIF_H__)