nsp_cs.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*=======================================================/
  2. Header file for nsp_cs.c
  3. By: YOKOTA Hiroshi <yokota@netlab.is.tsukuba.ac.jp>
  4. Ver.1.0 : Cut unused lines.
  5. Ver 0.1 : Initial version.
  6. This software may be used and distributed according to the terms of
  7. the GNU General Public License.
  8. =========================================================*/
  9. /* $Id: nsp_cs.h,v 1.19 2003/08/18 11:09:19 elca Exp $ */
  10. #ifndef __nsp_cs__
  11. #define __nsp_cs__
  12. /* for debugging */
  13. //#define NSP_DEBUG 9
  14. /*
  15. #define static
  16. #define inline
  17. */
  18. /************************************
  19. * Some useful macros...
  20. */
  21. #define BIT(x) (1L << (x))
  22. /* SCSI initiator must be ID 7 */
  23. #define NSP_INITIATOR_ID 7
  24. #define NSP_SELTIMEOUT 200
  25. /***************************************************************************
  26. * register definitions
  27. ***************************************************************************/
  28. /*========================================================================
  29. * base register
  30. ========================================================================*/
  31. #define IRQCONTROL 0x00 /* R */
  32. # define IRQCONTROL_RESELECT_CLEAR BIT(0)
  33. # define IRQCONTROL_PHASE_CHANGE_CLEAR BIT(1)
  34. # define IRQCONTROL_TIMER_CLEAR BIT(2)
  35. # define IRQCONTROL_FIFO_CLEAR BIT(3)
  36. # define IRQCONTROL_ALLMASK 0xff
  37. # define IRQCONTROL_ALLCLEAR (IRQCONTROL_RESELECT_CLEAR | \
  38. IRQCONTROL_PHASE_CHANGE_CLEAR | \
  39. IRQCONTROL_TIMER_CLEAR | \
  40. IRQCONTROL_FIFO_CLEAR )
  41. # define IRQCONTROL_IRQDISABLE 0xf0
  42. #define IRQSTATUS 0x00 /* W */
  43. # define IRQSTATUS_SCSI BIT(0)
  44. # define IRQSTATUS_TIMER BIT(2)
  45. # define IRQSTATUS_FIFO BIT(3)
  46. # define IRQSTATUS_MASK 0x0f
  47. #define IFSELECT 0x01 /* W */
  48. # define IF_IFSEL BIT(0)
  49. # define IF_REGSEL BIT(2)
  50. #define FIFOSTATUS 0x01 /* R */
  51. # define FIFOSTATUS_CHIP_REVISION_MASK 0x0f
  52. # define FIFOSTATUS_CHIP_ID_MASK 0x70
  53. # define FIFOSTATUS_FULL_EMPTY BIT(7)
  54. #define INDEXREG 0x02 /* R/W */
  55. #define DATAREG 0x03 /* R/W */
  56. #define FIFODATA 0x04 /* R/W */
  57. #define FIFODATA1 0x05 /* R/W */
  58. #define FIFODATA2 0x06 /* R/W */
  59. #define FIFODATA3 0x07 /* R/W */
  60. /*====================================================================
  61. * indexed register
  62. ====================================================================*/
  63. #define EXTBUSCTRL 0x10 /* R/W,deleted */
  64. #define CLOCKDIV 0x11 /* R/W */
  65. # define CLOCK_40M 0x02
  66. # define CLOCK_20M 0x01
  67. # define FAST_20 BIT(2)
  68. #define TERMPWRCTRL 0x13 /* R/W */
  69. # define POWER_ON BIT(0)
  70. #define SCSIIRQMODE 0x15 /* R/W */
  71. # define SCSI_PHASE_CHANGE_EI BIT(0)
  72. # define RESELECT_EI BIT(4)
  73. # define FIFO_IRQ_EI BIT(5)
  74. # define SCSI_RESET_IRQ_EI BIT(6)
  75. #define IRQPHASESENCE 0x16 /* R */
  76. # define LATCHED_MSG BIT(0)
  77. # define LATCHED_IO BIT(1)
  78. # define LATCHED_CD BIT(2)
  79. # define LATCHED_BUS_FREE BIT(3)
  80. # define PHASE_CHANGE_IRQ BIT(4)
  81. # define RESELECT_IRQ BIT(5)
  82. # define FIFO_IRQ BIT(6)
  83. # define SCSI_RESET_IRQ BIT(7)
  84. #define TIMERCOUNT 0x17 /* R/W */
  85. #define SCSIBUSCTRL 0x18 /* R/W */
  86. # define SCSI_SEL BIT(0)
  87. # define SCSI_RST BIT(1)
  88. # define SCSI_DATAOUT_ENB BIT(2)
  89. # define SCSI_ATN BIT(3)
  90. # define SCSI_ACK BIT(4)
  91. # define SCSI_BSY BIT(5)
  92. # define AUTODIRECTION BIT(6)
  93. # define ACKENB BIT(7)
  94. #define SCSIBUSMON 0x19 /* R */
  95. #define SETARBIT 0x1A /* W */
  96. # define ARBIT_GO BIT(0)
  97. # define ARBIT_FLAG_CLEAR BIT(1)
  98. #define ARBITSTATUS 0x1A /* R */
  99. /*# define ARBIT_GO BIT(0)*/
  100. # define ARBIT_WIN BIT(1)
  101. # define ARBIT_FAIL BIT(2)
  102. # define RESELECT_FLAG BIT(3)
  103. #define PARITYCTRL 0x1B /* W */
  104. #define PARITYSTATUS 0x1B /* R */
  105. #define COMMANDCTRL 0x1C /* W */
  106. # define CLEAR_COMMAND_POINTER BIT(0)
  107. # define AUTO_COMMAND_GO BIT(1)
  108. #define RESELECTID 0x1C /* R */
  109. #define COMMANDDATA 0x1D /* R/W */
  110. #define POINTERCLR 0x1E /* W */
  111. # define POINTER_CLEAR BIT(0)
  112. # define ACK_COUNTER_CLEAR BIT(1)
  113. # define REQ_COUNTER_CLEAR BIT(2)
  114. # define HOST_COUNTER_CLEAR BIT(3)
  115. # define READ_SOURCE (BIT(4) | BIT(5))
  116. # define ACK_COUNTER (0)
  117. # define REQ_COUNTER (BIT(4))
  118. # define HOST_COUNTER (BIT(5))
  119. #define TRANSFERCOUNT 0x1E /* R */
  120. #define TRANSFERMODE 0x20 /* R/W */
  121. # define MODE_MEM8 BIT(0)
  122. # define MODE_MEM32 BIT(1)
  123. # define MODE_ADR24 BIT(2)
  124. # define MODE_ADR32 BIT(3)
  125. # define MODE_IO8 BIT(4)
  126. # define MODE_IO32 BIT(5)
  127. # define TRANSFER_GO BIT(6)
  128. # define BRAIND BIT(7)
  129. #define SYNCREG 0x21 /* R/W */
  130. # define SYNCREG_OFFSET_MASK 0x0f
  131. # define SYNCREG_PERIOD_MASK 0xf0
  132. # define SYNCREG_PERIOD_SHIFT 4
  133. #define SCSIDATALATCH 0x22 /* W */
  134. #define SCSIDATAIN 0x22 /* R */
  135. #define SCSIDATAWITHACK 0x23 /* R/W */
  136. #define SCAMCONTROL 0x24 /* W */
  137. #define SCAMSTATUS 0x24 /* R */
  138. #define SCAMDATA 0x25 /* R/W */
  139. #define OTHERCONTROL 0x26 /* R/W */
  140. # define TPL_ROM_WRITE_EN BIT(0)
  141. # define TPWR_OUT BIT(1)
  142. # define TPWR_SENSE BIT(2)
  143. # define RA8_CONTROL BIT(3)
  144. #define ACKWIDTH 0x27 /* R/W */
  145. #define CLRTESTPNT 0x28 /* W */
  146. #define ACKCNTLD 0x29 /* W */
  147. #define REQCNTLD 0x2A /* W */
  148. #define HSTCNTLD 0x2B /* W */
  149. #define CHECKSUM 0x2C /* R/W */
  150. /************************************************************************
  151. * Input status bit definitions.
  152. ************************************************************************/
  153. #define S_MESSAGE BIT(0) /* Message line from SCSI bus */
  154. #define S_IO BIT(1) /* Input/Output line from SCSI bus */
  155. #define S_CD BIT(2) /* Command/Data line from SCSI bus */
  156. #define S_BUSY BIT(3) /* Busy line from SCSI bus */
  157. #define S_ACK BIT(4) /* Acknowlege line from SCSI bus */
  158. #define S_REQUEST BIT(5) /* Request line from SCSI bus */
  159. #define S_SELECT BIT(6) /* */
  160. #define S_ATN BIT(7) /* */
  161. /***********************************************************************
  162. * Useful Bus Monitor status combinations.
  163. ***********************************************************************/
  164. #define BUSMON_SEL S_SELECT
  165. #define BUSMON_BSY S_BUSY
  166. #define BUSMON_REQ S_REQUEST
  167. #define BUSMON_IO S_IO
  168. #define BUSMON_ACK S_ACK
  169. #define BUSMON_BUS_FREE 0
  170. #define BUSMON_COMMAND ( S_BUSY | S_CD | S_REQUEST )
  171. #define BUSMON_MESSAGE_IN ( S_BUSY | S_CD | S_IO | S_MESSAGE | S_REQUEST )
  172. #define BUSMON_MESSAGE_OUT ( S_BUSY | S_CD | S_MESSAGE | S_REQUEST )
  173. #define BUSMON_DATA_IN ( S_BUSY | S_IO | S_REQUEST )
  174. #define BUSMON_DATA_OUT ( S_BUSY | S_REQUEST )
  175. #define BUSMON_STATUS ( S_BUSY | S_CD | S_IO | S_REQUEST )
  176. #define BUSMON_SELECT ( S_IO | S_SELECT )
  177. #define BUSMON_RESELECT ( S_IO | S_SELECT )
  178. #define BUSMON_PHASE_MASK ( S_CD | S_IO | S_MESSAGE | S_SELECT )
  179. #define BUSPHASE_SELECT ( BUSMON_SELECT & BUSMON_PHASE_MASK )
  180. #define BUSPHASE_COMMAND ( BUSMON_COMMAND & BUSMON_PHASE_MASK )
  181. #define BUSPHASE_MESSAGE_IN ( BUSMON_MESSAGE_IN & BUSMON_PHASE_MASK )
  182. #define BUSPHASE_MESSAGE_OUT ( BUSMON_MESSAGE_OUT & BUSMON_PHASE_MASK )
  183. #define BUSPHASE_DATA_IN ( BUSMON_DATA_IN & BUSMON_PHASE_MASK )
  184. #define BUSPHASE_DATA_OUT ( BUSMON_DATA_OUT & BUSMON_PHASE_MASK )
  185. #define BUSPHASE_STATUS ( BUSMON_STATUS & BUSMON_PHASE_MASK )
  186. /*====================================================================*/
  187. typedef struct scsi_info_t {
  188. struct pcmcia_device *p_dev;
  189. struct Scsi_Host *host;
  190. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,74))
  191. dev_node_t node;
  192. #else
  193. int ndev;
  194. dev_node_t node[8];
  195. struct bus_operations *bus;
  196. #endif
  197. int stop;
  198. } scsi_info_t;
  199. /* synchronous transfer negotiation data */
  200. typedef struct _sync_data {
  201. unsigned int SyncNegotiation;
  202. #define SYNC_NOT_YET 0
  203. #define SYNC_OK 1
  204. #define SYNC_NG 2
  205. unsigned int SyncPeriod;
  206. unsigned int SyncOffset;
  207. unsigned char SyncRegister;
  208. unsigned char AckWidth;
  209. } sync_data;
  210. typedef struct _nsp_hw_data {
  211. unsigned int BaseAddress;
  212. unsigned int NumAddress;
  213. unsigned int IrqNumber;
  214. unsigned long MmioAddress;
  215. #define NSP_MMIO_OFFSET 0x0800
  216. unsigned long MmioLength;
  217. unsigned char ScsiClockDiv;
  218. unsigned char TransferMode;
  219. int TimerCount;
  220. int SelectionTimeOut;
  221. Scsi_Cmnd *CurrentSC;
  222. //int CurrnetTarget;
  223. int FifoCount;
  224. #define MSGBUF_SIZE 20
  225. unsigned char MsgBuffer[MSGBUF_SIZE];
  226. int MsgLen;
  227. #define N_TARGET 8
  228. sync_data Sync[N_TARGET];
  229. char nspinfo[110]; /* description */
  230. spinlock_t Lock;
  231. scsi_info_t *ScsiInfo; /* attach <-> detect glue */
  232. #ifdef NSP_DEBUG
  233. int CmdId; /* Accepted command serial number.
  234. Used for debugging. */
  235. #endif
  236. } nsp_hw_data;
  237. /****************************************************************************
  238. *
  239. */
  240. /* Card service functions */
  241. static void nsp_cs_detach (struct pcmcia_device *p_dev);
  242. static void nsp_cs_release(struct pcmcia_device *link);
  243. static int nsp_cs_config (struct pcmcia_device *link);
  244. /* Linux SCSI subsystem specific functions */
  245. static struct Scsi_Host *nsp_detect (struct scsi_host_template *sht);
  246. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
  247. static int nsp_detect_old (struct scsi_host_template *sht);
  248. static int nsp_release_old(struct Scsi_Host *shpnt);
  249. #endif
  250. static const char *nsp_info (struct Scsi_Host *shpnt);
  251. static int nsp_proc_info (
  252. #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  253. struct Scsi_Host *host,
  254. #endif
  255. char *buffer,
  256. char **start,
  257. off_t offset,
  258. int length,
  259. #if !(LINUX_VERSION_CODE > KERNEL_VERSION(2,5,73))
  260. int hostno,
  261. #endif
  262. int inout);
  263. static int nsp_queuecommand(Scsi_Cmnd *SCpnt, void (* done)(Scsi_Cmnd *SCpnt));
  264. /* Error handler */
  265. /*static int nsp_eh_abort (Scsi_Cmnd *SCpnt);*/
  266. /*static int nsp_eh_device_reset(Scsi_Cmnd *SCpnt);*/
  267. static int nsp_eh_bus_reset (Scsi_Cmnd *SCpnt);
  268. static int nsp_eh_host_reset (Scsi_Cmnd *SCpnt);
  269. static int nsp_bus_reset (nsp_hw_data *data);
  270. /* */
  271. static int nsphw_init (nsp_hw_data *data);
  272. static int nsphw_start_selection(Scsi_Cmnd *SCpnt);
  273. static void nsp_start_timer (Scsi_Cmnd *SCpnt, int time);
  274. static int nsp_fifo_count (Scsi_Cmnd *SCpnt);
  275. static void nsp_pio_read (Scsi_Cmnd *SCpnt);
  276. static void nsp_pio_write (Scsi_Cmnd *SCpnt);
  277. static int nsp_nexus (Scsi_Cmnd *SCpnt);
  278. static void nsp_scsi_done (Scsi_Cmnd *SCpnt);
  279. static int nsp_analyze_sdtr (Scsi_Cmnd *SCpnt);
  280. static int nsp_negate_signal (Scsi_Cmnd *SCpnt, unsigned char mask, char *str);
  281. static int nsp_expect_signal (Scsi_Cmnd *SCpnt, unsigned char current_phase, unsigned char mask);
  282. static int nsp_xfer (Scsi_Cmnd *SCpnt, int phase);
  283. static int nsp_dataphase_bypass (Scsi_Cmnd *SCpnt);
  284. static int nsp_reselected (Scsi_Cmnd *SCpnt);
  285. static struct Scsi_Host *nsp_detect(struct scsi_host_template *sht);
  286. /* Interrupt handler */
  287. //static irqreturn_t nspintr(int irq, void *dev_id);
  288. /* Module entry point*/
  289. static int __init nsp_cs_init(void);
  290. static void __exit nsp_cs_exit(void);
  291. /* Debug */
  292. #ifdef NSP_DEBUG
  293. static void show_command (Scsi_Cmnd *SCpnt);
  294. static void show_phase (Scsi_Cmnd *SCpnt);
  295. static void show_busphase(unsigned char stat);
  296. static void show_message (nsp_hw_data *data);
  297. #else
  298. # define show_command(ptr) /* */
  299. # define show_phase(SCpnt) /* */
  300. # define show_busphase(stat) /* */
  301. # define show_message(data) /* */
  302. #endif
  303. /*
  304. * SCSI phase
  305. */
  306. enum _scsi_phase {
  307. PH_UNDETERMINED ,
  308. PH_ARBSTART ,
  309. PH_SELSTART ,
  310. PH_SELECTED ,
  311. PH_COMMAND ,
  312. PH_DATA ,
  313. PH_STATUS ,
  314. PH_MSG_IN ,
  315. PH_MSG_OUT ,
  316. PH_DISCONNECT ,
  317. PH_RESELECT ,
  318. PH_ABORT ,
  319. PH_RESET
  320. };
  321. enum _data_in_out {
  322. IO_UNKNOWN,
  323. IO_IN,
  324. IO_OUT
  325. };
  326. enum _burst_mode {
  327. BURST_IO8 = 0,
  328. BURST_IO32 = 1,
  329. BURST_MEM32 = 2,
  330. };
  331. /**************************************************************************
  332. * SCSI messaage
  333. */
  334. #define MSG_COMMAND_COMPLETE 0x00
  335. #define MSG_EXTENDED 0x01
  336. #define MSG_ABORT 0x06
  337. #define MSG_NO_OPERATION 0x08
  338. #define MSG_BUS_DEVICE_RESET 0x0c
  339. #define MSG_EXT_SDTR 0x01
  340. /**************************************************************************
  341. * Compatibility functions
  342. */
  343. /* for Kernel 2.4 */
  344. #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
  345. # define scsi_register_host(template) scsi_register_module(MODULE_SCSI_HA, template)
  346. # define scsi_unregister_host(template) scsi_unregister_module(MODULE_SCSI_HA, template)
  347. # define scsi_host_put(host) scsi_unregister(host)
  348. typedef void irqreturn_t;
  349. # define IRQ_NONE /* */
  350. # define IRQ_HANDLED /* */
  351. # define IRQ_RETVAL(x) /* */
  352. /* This is ad-hoc version of scsi_host_get_next() */
  353. static inline struct Scsi_Host *scsi_host_get_next(struct Scsi_Host *host)
  354. {
  355. if (host == NULL) {
  356. return scsi_hostlist;
  357. } else {
  358. return host->next;
  359. }
  360. }
  361. /* This is ad-hoc version of scsi_host_hn_get() */
  362. static inline struct Scsi_Host *scsi_host_hn_get(unsigned short hostno)
  363. {
  364. struct Scsi_Host *host;
  365. for (host = scsi_host_get_next(NULL); host != NULL;
  366. host = scsi_host_get_next(host)) {
  367. if (host->host_no == hostno) {
  368. break;
  369. }
  370. }
  371. return host;
  372. }
  373. static void cs_error(struct pcmcia_device *handle, int func, int ret)
  374. {
  375. error_info_t err = { func, ret };
  376. pcmcia_report_error(handle, &err);
  377. }
  378. /* scatter-gather table */
  379. # define BUFFER_ADDR (SCpnt->SCp.buffer->address)
  380. #endif
  381. /* for Kernel 2.6 */
  382. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
  383. /* scatter-gather table */
  384. # define BUFFER_ADDR ((char *)((unsigned int)(SCpnt->SCp.buffer->page) + SCpnt->SCp.buffer->offset))
  385. #endif
  386. #endif /*__nsp_cs__*/
  387. /* end */