spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright (c) 2001 Navin Boppuri / Prashant Patel
  3. * <nboppuri@trinetcommunication.com>,
  4. * <pmpatel@trinetcommunication.com>
  5. * Copyright (c) 2001 Gerd Mennchen <Gerd.Mennchen@icn.siemens.de>
  6. * Copyright (c) 2001-2003 Wolfgang Denk, DENX Software Engineering, <wd@denx.de>.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. /*
  27. * MPC8260 CPM SPI interface.
  28. *
  29. * Parts of this code are probably not portable and/or specific to
  30. * the board which I used for the tests. Please send fixes/complaints
  31. * to wd@denx.de
  32. *
  33. */
  34. #include <common.h>
  35. #include <asm/cpm_8260.h>
  36. #include <linux/ctype.h>
  37. #include <malloc.h>
  38. #include <post.h>
  39. #include <net.h>
  40. #if defined(CONFIG_SPI)
  41. /* Warning:
  42. * You cannot enable DEBUG for early system initalization, i. e. when
  43. * this driver is used to read environment parameters like "baudrate"
  44. * from EEPROM which are used to initialize the serial port which is
  45. * needed to print the debug messages...
  46. */
  47. #undef DEBUG
  48. #define SPI_EEPROM_WREN 0x06
  49. #define SPI_EEPROM_RDSR 0x05
  50. #define SPI_EEPROM_READ 0x03
  51. #define SPI_EEPROM_WRITE 0x02
  52. /* ---------------------------------------------------------------
  53. * Offset for initial SPI buffers in DPRAM:
  54. * We need a 520 byte scratch DPRAM area to use at an early stage.
  55. * It is used between the two initialization calls (spi_init_f()
  56. * and spi_init_r()).
  57. * The value 0x2000 makes it far enough from the start of the data
  58. * area (as well as from the stack pointer).
  59. * --------------------------------------------------------------- */
  60. #ifndef CONFIG_SYS_SPI_INIT_OFFSET
  61. #define CONFIG_SYS_SPI_INIT_OFFSET 0x2000
  62. #endif
  63. #define CPM_SPI_BASE 0x100
  64. #ifdef DEBUG
  65. #define DPRINT(a) printf a;
  66. /* -----------------------------------------------
  67. * Helper functions to peek into tx and rx buffers
  68. * ----------------------------------------------- */
  69. static const char * const hex_digit = "0123456789ABCDEF";
  70. static char quickhex (int i)
  71. {
  72. return hex_digit[i];
  73. }
  74. static void memdump (void *pv, int num)
  75. {
  76. int i;
  77. unsigned char *pc = (unsigned char *) pv;
  78. for (i = 0; i < num; i++)
  79. printf ("%c%c ", quickhex (pc[i] >> 4), quickhex (pc[i] & 0x0f));
  80. printf ("\t");
  81. for (i = 0; i < num; i++)
  82. printf ("%c", isprint (pc[i]) ? pc[i] : '.');
  83. printf ("\n");
  84. }
  85. #else /* !DEBUG */
  86. #define DPRINT(a)
  87. #endif /* DEBUG */
  88. /* -------------------
  89. * Function prototypes
  90. * ------------------- */
  91. void spi_init (void);
  92. ssize_t spi_read (uchar *, int, uchar *, int);
  93. ssize_t spi_write (uchar *, int, uchar *, int);
  94. ssize_t spi_xfer (size_t);
  95. /* -------------------
  96. * Variables
  97. * ------------------- */
  98. #define MAX_BUFFER 0x104
  99. /* ----------------------------------------------------------------------
  100. * Initially we place the RX and TX buffers at a fixed location in DPRAM!
  101. * ---------------------------------------------------------------------- */
  102. static uchar *rxbuf =
  103. (uchar *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
  104. [CONFIG_SYS_SPI_INIT_OFFSET];
  105. static uchar *txbuf =
  106. (uchar *)&((immap_t *)CONFIG_SYS_IMMR)->im_dprambase
  107. [CONFIG_SYS_SPI_INIT_OFFSET+MAX_BUFFER];
  108. /* **************************************************************************
  109. *
  110. * Function: spi_init_f
  111. *
  112. * Description: Init SPI-Controller (ROM part)
  113. *
  114. * return: ---
  115. *
  116. * *********************************************************************** */
  117. void spi_init_f (void)
  118. {
  119. unsigned int dpaddr;
  120. volatile spi_t *spi;
  121. volatile immap_t *immr;
  122. volatile cpm8260_t *cp;
  123. volatile cbd_t *tbdf, *rbdf;
  124. immr = (immap_t *) CONFIG_SYS_IMMR;
  125. cp = (cpm8260_t *) &immr->im_cpm;
  126. *(ushort *)(&immr->im_dprambase[PROFF_SPI_BASE]) = PROFF_SPI;
  127. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  128. /* 1 */
  129. /* ------------------------------------------------
  130. * Initialize Port D SPI pins
  131. * (we are only in Master Mode !)
  132. * ------------------------------------------------ */
  133. /* --------------------------------------------
  134. * GPIO or per. Function
  135. * PPARD[16] = 1 [0x00008000] (SPIMISO)
  136. * PPARD[17] = 1 [0x00004000] (SPIMOSI)
  137. * PPARD[18] = 1 [0x00002000] (SPICLK)
  138. * PPARD[12] = 0 [0x00080000] -> GPIO: (CS for ATC EEPROM)
  139. * -------------------------------------------- */
  140. immr->im_ioport.iop_ppard |= 0x0000E000; /* set bits */
  141. immr->im_ioport.iop_ppard &= ~0x00080000; /* reset bit */
  142. /* ----------------------------------------------
  143. * In/Out or per. Function 0/1
  144. * PDIRD[16] = 0 [0x00008000] -> PERI1: SPIMISO
  145. * PDIRD[17] = 0 [0x00004000] -> PERI1: SPIMOSI
  146. * PDIRD[18] = 0 [0x00002000] -> PERI1: SPICLK
  147. * PDIRD[12] = 1 [0x00080000] -> GPIO OUT: CS for ATC EEPROM
  148. * ---------------------------------------------- */
  149. immr->im_ioport.iop_pdird &= ~0x0000E000;
  150. immr->im_ioport.iop_pdird |= 0x00080000;
  151. /* ----------------------------------------------
  152. * special option reg.
  153. * PSORD[16] = 1 [0x00008000] -> SPIMISO
  154. * PSORD[17] = 1 [0x00004000] -> SPIMOSI
  155. * PSORD[18] = 1 [0x00002000] -> SPICLK
  156. * ---------------------------------------------- */
  157. immr->im_ioport.iop_psord |= 0x0000E000;
  158. /* Initialize the parameter ram.
  159. * We need to make sure many things are initialized to zero
  160. */
  161. spi->spi_rstate = 0;
  162. spi->spi_rdp = 0;
  163. spi->spi_rbptr = 0;
  164. spi->spi_rbc = 0;
  165. spi->spi_rxtmp = 0;
  166. spi->spi_tstate = 0;
  167. spi->spi_tdp = 0;
  168. spi->spi_tbptr = 0;
  169. spi->spi_tbc = 0;
  170. spi->spi_txtmp = 0;
  171. /* Allocate space for one transmit and one receive buffer
  172. * descriptor in the DP ram
  173. */
  174. #ifdef CONFIG_SYS_ALLOC_DPRAM
  175. dpaddr = m8260_cpm_dpalloc (sizeof(cbd_t)*2, 8);
  176. #else
  177. dpaddr = CPM_SPI_BASE;
  178. #endif
  179. /* 3 */
  180. /* Set up the SPI parameters in the parameter ram */
  181. spi->spi_rbase = dpaddr;
  182. spi->spi_tbase = dpaddr + sizeof (cbd_t);
  183. /***********IMPORTANT******************/
  184. /*
  185. * Setting transmit and receive buffer descriptor pointers
  186. * initially to rbase and tbase. Only the microcode patches
  187. * documentation talks about initializing this pointer. This
  188. * is missing from the sample I2C driver. If you dont
  189. * initialize these pointers, the kernel hangs.
  190. */
  191. spi->spi_rbptr = spi->spi_rbase;
  192. spi->spi_tbptr = spi->spi_tbase;
  193. /* 4 */
  194. /* Init SPI Tx + Rx Parameters */
  195. while (cp->cp_cpcr & CPM_CR_FLG)
  196. ;
  197. cp->cp_cpcr = mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK,
  198. 0, CPM_CR_INIT_TRX) | CPM_CR_FLG;
  199. while (cp->cp_cpcr & CPM_CR_FLG)
  200. ;
  201. /* 6 */
  202. /* Set to big endian. */
  203. spi->spi_tfcr = CPMFCR_EB;
  204. spi->spi_rfcr = CPMFCR_EB;
  205. /* 7 */
  206. /* Set maximum receive size. */
  207. spi->spi_mrblr = MAX_BUFFER;
  208. /* 8 + 9 */
  209. /* tx and rx buffer descriptors */
  210. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  211. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  212. tbdf->cbd_sc &= ~BD_SC_READY;
  213. rbdf->cbd_sc &= ~BD_SC_EMPTY;
  214. /* Set the bd's rx and tx buffer address pointers */
  215. rbdf->cbd_bufaddr = (ulong) rxbuf;
  216. tbdf->cbd_bufaddr = (ulong) txbuf;
  217. /* 10 + 11 */
  218. immr->im_spi.spi_spie = SPI_EMASK; /* Clear all SPI events */
  219. immr->im_spi.spi_spim = 0x00; /* Mask all SPI events */
  220. return;
  221. }
  222. /* **************************************************************************
  223. *
  224. * Function: spi_init_r
  225. *
  226. * Description: Init SPI-Controller (RAM part) -
  227. * The malloc engine is ready and we can move our buffers to
  228. * normal RAM
  229. *
  230. * return: ---
  231. *
  232. * *********************************************************************** */
  233. void spi_init_r (void)
  234. {
  235. volatile spi_t *spi;
  236. volatile immap_t *immr;
  237. volatile cpm8260_t *cp;
  238. volatile cbd_t *tbdf, *rbdf;
  239. immr = (immap_t *) CONFIG_SYS_IMMR;
  240. cp = (cpm8260_t *) &immr->im_cpm;
  241. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  242. /* tx and rx buffer descriptors */
  243. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  244. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  245. /* Allocate memory for RX and TX buffers */
  246. rxbuf = (uchar *) malloc (MAX_BUFFER);
  247. txbuf = (uchar *) malloc (MAX_BUFFER);
  248. rbdf->cbd_bufaddr = (ulong) rxbuf;
  249. tbdf->cbd_bufaddr = (ulong) txbuf;
  250. return;
  251. }
  252. /****************************************************************************
  253. * Function: spi_write
  254. **************************************************************************** */
  255. ssize_t spi_write (uchar *addr, int alen, uchar *buffer, int len)
  256. {
  257. int i;
  258. memset(rxbuf, 0, MAX_BUFFER);
  259. memset(txbuf, 0, MAX_BUFFER);
  260. *txbuf = SPI_EEPROM_WREN; /* write enable */
  261. spi_xfer(1);
  262. memcpy(txbuf, addr, alen);
  263. *txbuf = SPI_EEPROM_WRITE; /* WRITE memory array */
  264. memcpy(alen + txbuf, buffer, len);
  265. spi_xfer(alen + len);
  266. /* ignore received data */
  267. for (i = 0; i < 1000; i++) {
  268. *txbuf = SPI_EEPROM_RDSR; /* read status */
  269. txbuf[1] = 0;
  270. spi_xfer(2);
  271. if (!(rxbuf[1] & 1)) {
  272. break;
  273. }
  274. udelay(1000);
  275. }
  276. if (i >= 1000) {
  277. printf ("*** spi_write: Time out while writing!\n");
  278. }
  279. return len;
  280. }
  281. /****************************************************************************
  282. * Function: spi_read
  283. **************************************************************************** */
  284. ssize_t spi_read (uchar *addr, int alen, uchar *buffer, int len)
  285. {
  286. memset(rxbuf, 0, MAX_BUFFER);
  287. memset(txbuf, 0, MAX_BUFFER);
  288. memcpy(txbuf, addr, alen);
  289. *txbuf = SPI_EEPROM_READ; /* READ memory array */
  290. /*
  291. * There is a bug in 860T (?) that cuts the last byte of input
  292. * if we're reading into DPRAM. The solution we choose here is
  293. * to always read len+1 bytes (we have one extra byte at the
  294. * end of the buffer).
  295. */
  296. spi_xfer(alen + len + 1);
  297. memcpy(buffer, alen + rxbuf, len);
  298. return len;
  299. }
  300. /****************************************************************************
  301. * Function: spi_xfer
  302. **************************************************************************** */
  303. ssize_t spi_xfer (size_t count)
  304. {
  305. volatile immap_t *immr;
  306. volatile cpm8260_t *cp;
  307. volatile spi_t *spi;
  308. cbd_t *tbdf, *rbdf;
  309. int tm;
  310. DPRINT (("*** spi_xfer entered ***\n"));
  311. immr = (immap_t *) CONFIG_SYS_IMMR;
  312. cp = (cpm8260_t *) &immr->im_cpm;
  313. spi = (spi_t *)&immr->im_dprambase[PROFF_SPI];
  314. tbdf = (cbd_t *) & immr->im_dprambase[spi->spi_tbase];
  315. rbdf = (cbd_t *) & immr->im_dprambase[spi->spi_rbase];
  316. /* Board-specific: Set CS for device (ATC EEPROM) */
  317. immr->im_ioport.iop_pdatd &= ~0x00080000;
  318. /* Setting tx bd status and data length */
  319. tbdf->cbd_sc = BD_SC_READY | BD_SC_LAST | BD_SC_WRAP;
  320. tbdf->cbd_datlen = count;
  321. DPRINT (("*** spi_xfer: Bytes to be xferred: %d ***\n",
  322. tbdf->cbd_datlen));
  323. /* Setting rx bd status and data length */
  324. rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP;
  325. rbdf->cbd_datlen = 0; /* rx length has no significance */
  326. immr->im_spi.spi_spmode = SPMODE_REV |
  327. SPMODE_MSTR |
  328. SPMODE_EN |
  329. SPMODE_LEN(8) | /* 8 Bits per char */
  330. SPMODE_PM(0x8) ; /* medium speed */
  331. immr->im_spi.spi_spie = SPI_EMASK; /* Clear all SPI events */
  332. immr->im_spi.spi_spim = 0x00; /* Mask all SPI events */
  333. /* start spi transfer */
  334. DPRINT (("*** spi_xfer: Performing transfer ...\n"));
  335. immr->im_spi.spi_spcom |= SPI_STR; /* Start transmit */
  336. /* --------------------------------
  337. * Wait for SPI transmit to get out
  338. * or time out (1 second = 1000 ms)
  339. * -------------------------------- */
  340. for (tm=0; tm<1000; ++tm) {
  341. if (immr->im_spi.spi_spie & SPI_TXB) { /* Tx Buffer Empty */
  342. DPRINT (("*** spi_xfer: Tx buffer empty\n"));
  343. break;
  344. }
  345. if ((tbdf->cbd_sc & BD_SC_READY) == 0) {
  346. DPRINT (("*** spi_xfer: Tx BD done\n"));
  347. break;
  348. }
  349. udelay (1000);
  350. }
  351. if (tm >= 1000) {
  352. printf ("*** spi_xfer: Time out while xferring to/from SPI!\n");
  353. }
  354. DPRINT (("*** spi_xfer: ... transfer ended\n"));
  355. #ifdef DEBUG
  356. printf ("\nspi_xfer: txbuf after xfer\n");
  357. memdump ((void *) txbuf, 16); /* dump of txbuf before transmit */
  358. printf ("spi_xfer: rxbuf after xfer\n");
  359. memdump ((void *) rxbuf, 16); /* dump of rxbuf after transmit */
  360. printf ("\n");
  361. #endif
  362. /* Clear CS for device */
  363. immr->im_ioport.iop_pdatd |= 0x00080000;
  364. return count;
  365. }
  366. #endif /* CONFIG_SPI */