davinci_spi.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  1. /*
  2. * Copyright (C) 2009 Texas Instruments.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #include <linux/interrupt.h>
  19. #include <linux/io.h>
  20. #include <linux/gpio.h>
  21. #include <linux/module.h>
  22. #include <linux/delay.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/err.h>
  25. #include <linux/clk.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/spi/spi.h>
  28. #include <linux/spi/spi_bitbang.h>
  29. #include <linux/slab.h>
  30. #include <mach/spi.h>
  31. #include <mach/edma.h>
  32. #define SPI_NO_RESOURCE ((resource_size_t)-1)
  33. #define SPI_MAX_CHIPSELECT 2
  34. #define CS_DEFAULT 0xFF
  35. #define SPI_BUFSIZ (SMP_CACHE_BYTES + 1)
  36. #define SPIFMT_PHASE_MASK BIT(16)
  37. #define SPIFMT_POLARITY_MASK BIT(17)
  38. #define SPIFMT_DISTIMER_MASK BIT(18)
  39. #define SPIFMT_SHIFTDIR_MASK BIT(20)
  40. #define SPIFMT_WAITENA_MASK BIT(21)
  41. #define SPIFMT_PARITYENA_MASK BIT(22)
  42. #define SPIFMT_ODD_PARITY_MASK BIT(23)
  43. #define SPIFMT_WDELAY_MASK 0x3f000000u
  44. #define SPIFMT_WDELAY_SHIFT 24
  45. #define SPIFMT_PRESCALE_SHIFT 8
  46. /* SPIPC0 */
  47. #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */
  48. #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */
  49. #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */
  50. #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */
  51. #define SPIINT_MASKALL 0x0101035F
  52. #define SPI_INTLVL_1 0x000001FFu
  53. #define SPI_INTLVL_0 0x00000000u
  54. /* SPIDAT1 (upper 16 bit defines) */
  55. #define SPIDAT1_CSHOLD_MASK BIT(12)
  56. /* SPIGCR1 */
  57. #define SPIGCR1_CLKMOD_MASK BIT(1)
  58. #define SPIGCR1_MASTER_MASK BIT(0)
  59. #define SPIGCR1_LOOPBACK_MASK BIT(16)
  60. #define SPIGCR1_SPIENA_MASK BIT(24)
  61. /* SPIBUF */
  62. #define SPIBUF_TXFULL_MASK BIT(29)
  63. #define SPIBUF_RXEMPTY_MASK BIT(31)
  64. /* SPIDELAY */
  65. #define SPIDELAY_C2TDELAY_SHIFT 24
  66. #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT)
  67. #define SPIDELAY_T2CDELAY_SHIFT 16
  68. #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT)
  69. #define SPIDELAY_T2EDELAY_SHIFT 8
  70. #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT)
  71. #define SPIDELAY_C2EDELAY_SHIFT 0
  72. #define SPIDELAY_C2EDELAY_MASK 0xFF
  73. /* Error Masks */
  74. #define SPIFLG_DLEN_ERR_MASK BIT(0)
  75. #define SPIFLG_TIMEOUT_MASK BIT(1)
  76. #define SPIFLG_PARERR_MASK BIT(2)
  77. #define SPIFLG_DESYNC_MASK BIT(3)
  78. #define SPIFLG_BITERR_MASK BIT(4)
  79. #define SPIFLG_OVRRUN_MASK BIT(6)
  80. #define SPIFLG_RX_INTR_MASK BIT(8)
  81. #define SPIFLG_TX_INTR_MASK BIT(9)
  82. #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24)
  83. #define SPIINT_BITERR_INTR BIT(4)
  84. #define SPIINT_OVRRUN_INTR BIT(6)
  85. #define SPIINT_RX_INTR BIT(8)
  86. #define SPIINT_TX_INTR BIT(9)
  87. #define SPIINT_DMA_REQ_EN BIT(16)
  88. /* SPI Controller registers */
  89. #define SPIGCR0 0x00
  90. #define SPIGCR1 0x04
  91. #define SPIINT 0x08
  92. #define SPILVL 0x0c
  93. #define SPIFLG 0x10
  94. #define SPIPC0 0x14
  95. #define SPIDAT1 0x3c
  96. #define SPIBUF 0x40
  97. #define SPIDELAY 0x48
  98. #define SPIDEF 0x4c
  99. #define SPIFMT0 0x50
  100. /* We have 2 DMA channels per CS, one for RX and one for TX */
  101. struct davinci_spi_dma {
  102. int dma_tx_channel;
  103. int dma_rx_channel;
  104. int dma_tx_sync_dev;
  105. int dma_rx_sync_dev;
  106. enum dma_event_q eventq;
  107. struct completion dma_tx_completion;
  108. struct completion dma_rx_completion;
  109. };
  110. /* SPI Controller driver's private data. */
  111. struct davinci_spi {
  112. struct spi_bitbang bitbang;
  113. struct clk *clk;
  114. u8 version;
  115. resource_size_t pbase;
  116. void __iomem *base;
  117. size_t region_size;
  118. u32 irq;
  119. struct completion done;
  120. const void *tx;
  121. void *rx;
  122. u8 *tmp_buf;
  123. int count;
  124. struct davinci_spi_dma *dma_channels;
  125. struct davinci_spi_platform_data *pdata;
  126. void (*get_rx)(u32 rx_data, struct davinci_spi *);
  127. u32 (*get_tx)(struct davinci_spi *);
  128. u8 bytes_per_word[SPI_MAX_CHIPSELECT];
  129. };
  130. static struct davinci_spi_config davinci_spi_default_cfg;
  131. static unsigned use_dma;
  132. static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi)
  133. {
  134. u8 *rx = davinci_spi->rx;
  135. *rx++ = (u8)data;
  136. davinci_spi->rx = rx;
  137. }
  138. static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi)
  139. {
  140. u16 *rx = davinci_spi->rx;
  141. *rx++ = (u16)data;
  142. davinci_spi->rx = rx;
  143. }
  144. static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi)
  145. {
  146. u32 data;
  147. const u8 *tx = davinci_spi->tx;
  148. data = *tx++;
  149. davinci_spi->tx = tx;
  150. return data;
  151. }
  152. static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi)
  153. {
  154. u32 data;
  155. const u16 *tx = davinci_spi->tx;
  156. data = *tx++;
  157. davinci_spi->tx = tx;
  158. return data;
  159. }
  160. static inline void set_io_bits(void __iomem *addr, u32 bits)
  161. {
  162. u32 v = ioread32(addr);
  163. v |= bits;
  164. iowrite32(v, addr);
  165. }
  166. static inline void clear_io_bits(void __iomem *addr, u32 bits)
  167. {
  168. u32 v = ioread32(addr);
  169. v &= ~bits;
  170. iowrite32(v, addr);
  171. }
  172. static void davinci_spi_set_dma_req(const struct spi_device *spi, int enable)
  173. {
  174. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  175. if (enable)
  176. set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  177. else
  178. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN);
  179. }
  180. /*
  181. * Interface to control the chip select signal
  182. */
  183. static void davinci_spi_chipselect(struct spi_device *spi, int value)
  184. {
  185. struct davinci_spi *davinci_spi;
  186. struct davinci_spi_platform_data *pdata;
  187. u8 chip_sel = spi->chip_select;
  188. u16 spidat1_cfg = CS_DEFAULT;
  189. bool gpio_chipsel = false;
  190. davinci_spi = spi_master_get_devdata(spi->master);
  191. pdata = davinci_spi->pdata;
  192. if (pdata->chip_sel && chip_sel < pdata->num_chipselect &&
  193. pdata->chip_sel[chip_sel] != SPI_INTERN_CS)
  194. gpio_chipsel = true;
  195. /*
  196. * Board specific chip select logic decides the polarity and cs
  197. * line for the controller
  198. */
  199. if (gpio_chipsel) {
  200. if (value == BITBANG_CS_ACTIVE)
  201. gpio_set_value(pdata->chip_sel[chip_sel], 0);
  202. else
  203. gpio_set_value(pdata->chip_sel[chip_sel], 1);
  204. } else {
  205. if (value == BITBANG_CS_ACTIVE) {
  206. spidat1_cfg |= SPIDAT1_CSHOLD_MASK;
  207. spidat1_cfg &= ~(0x1 << chip_sel);
  208. }
  209. iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2);
  210. }
  211. }
  212. /**
  213. * davinci_spi_get_prescale - Calculates the correct prescale value
  214. * @maxspeed_hz: the maximum rate the SPI clock can run at
  215. *
  216. * This function calculates the prescale value that generates a clock rate
  217. * less than or equal to the specified maximum.
  218. *
  219. * Returns: calculated prescale - 1 for easy programming into SPI registers
  220. * or negative error number if valid prescalar cannot be updated.
  221. */
  222. static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi,
  223. u32 max_speed_hz)
  224. {
  225. int ret;
  226. ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz);
  227. if (ret < 3 || ret > 256)
  228. return -EINVAL;
  229. return ret - 1;
  230. }
  231. /**
  232. * davinci_spi_setup_transfer - This functions will determine transfer method
  233. * @spi: spi device on which data transfer to be done
  234. * @t: spi transfer in which transfer info is filled
  235. *
  236. * This function determines data transfer method (8/16/32 bit transfer).
  237. * It will also set the SPI Clock Control register according to
  238. * SPI slave device freq.
  239. */
  240. static int davinci_spi_setup_transfer(struct spi_device *spi,
  241. struct spi_transfer *t)
  242. {
  243. struct davinci_spi *davinci_spi;
  244. struct davinci_spi_config *spicfg;
  245. u8 bits_per_word = 0;
  246. u32 hz = 0, spifmt = 0, prescale = 0;
  247. davinci_spi = spi_master_get_devdata(spi->master);
  248. spicfg = (struct davinci_spi_config *)spi->controller_data;
  249. if (!spicfg)
  250. spicfg = &davinci_spi_default_cfg;
  251. if (t) {
  252. bits_per_word = t->bits_per_word;
  253. hz = t->speed_hz;
  254. }
  255. /* if bits_per_word is not set then set it default */
  256. if (!bits_per_word)
  257. bits_per_word = spi->bits_per_word;
  258. /*
  259. * Assign function pointer to appropriate transfer method
  260. * 8bit, 16bit or 32bit transfer
  261. */
  262. if (bits_per_word <= 8 && bits_per_word >= 2) {
  263. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  264. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  265. davinci_spi->bytes_per_word[spi->chip_select] = 1;
  266. } else if (bits_per_word <= 16 && bits_per_word >= 2) {
  267. davinci_spi->get_rx = davinci_spi_rx_buf_u16;
  268. davinci_spi->get_tx = davinci_spi_tx_buf_u16;
  269. davinci_spi->bytes_per_word[spi->chip_select] = 2;
  270. } else
  271. return -EINVAL;
  272. if (!hz)
  273. hz = spi->max_speed_hz;
  274. /* Set up SPIFMTn register, unique to this chipselect. */
  275. prescale = davinci_spi_get_prescale(davinci_spi, hz);
  276. if (prescale < 0)
  277. return prescale;
  278. spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f);
  279. if (spi->mode & SPI_LSB_FIRST)
  280. spifmt |= SPIFMT_SHIFTDIR_MASK;
  281. if (spi->mode & SPI_CPOL)
  282. spifmt |= SPIFMT_POLARITY_MASK;
  283. if (!(spi->mode & SPI_CPHA))
  284. spifmt |= SPIFMT_PHASE_MASK;
  285. /*
  286. * Version 1 hardware supports two basic SPI modes:
  287. * - Standard SPI mode uses 4 pins, with chipselect
  288. * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS)
  289. * (distinct from SPI_3WIRE, with just one data wire;
  290. * or similar variants without MOSI or without MISO)
  291. *
  292. * Version 2 hardware supports an optional handshaking signal,
  293. * so it can support two more modes:
  294. * - 5 pin SPI variant is standard SPI plus SPI_READY
  295. * - 4 pin with enable is (SPI_READY | SPI_NO_CS)
  296. */
  297. if (davinci_spi->version == SPI_VERSION_2) {
  298. u32 delay = 0;
  299. spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT)
  300. & SPIFMT_WDELAY_MASK);
  301. if (spicfg->odd_parity)
  302. spifmt |= SPIFMT_ODD_PARITY_MASK;
  303. if (spicfg->parity_enable)
  304. spifmt |= SPIFMT_PARITYENA_MASK;
  305. if (spicfg->timer_disable) {
  306. spifmt |= SPIFMT_DISTIMER_MASK;
  307. } else {
  308. delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT)
  309. & SPIDELAY_C2TDELAY_MASK;
  310. delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT)
  311. & SPIDELAY_T2CDELAY_MASK;
  312. }
  313. if (spi->mode & SPI_READY) {
  314. spifmt |= SPIFMT_WAITENA_MASK;
  315. delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT)
  316. & SPIDELAY_T2EDELAY_MASK;
  317. delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT)
  318. & SPIDELAY_C2EDELAY_MASK;
  319. }
  320. iowrite32(delay, davinci_spi->base + SPIDELAY);
  321. }
  322. iowrite32(spifmt, davinci_spi->base + SPIFMT0);
  323. return 0;
  324. }
  325. static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data)
  326. {
  327. struct spi_device *spi = (struct spi_device *)data;
  328. struct davinci_spi *davinci_spi;
  329. struct davinci_spi_dma *davinci_spi_dma;
  330. davinci_spi = spi_master_get_devdata(spi->master);
  331. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  332. if (ch_status == DMA_COMPLETE)
  333. edma_stop(davinci_spi_dma->dma_rx_channel);
  334. else
  335. edma_clean_channel(davinci_spi_dma->dma_rx_channel);
  336. complete(&davinci_spi_dma->dma_rx_completion);
  337. /* We must disable the DMA RX request */
  338. davinci_spi_set_dma_req(spi, 0);
  339. }
  340. static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data)
  341. {
  342. struct spi_device *spi = (struct spi_device *)data;
  343. struct davinci_spi *davinci_spi;
  344. struct davinci_spi_dma *davinci_spi_dma;
  345. davinci_spi = spi_master_get_devdata(spi->master);
  346. davinci_spi_dma = &(davinci_spi->dma_channels[spi->chip_select]);
  347. if (ch_status == DMA_COMPLETE)
  348. edma_stop(davinci_spi_dma->dma_tx_channel);
  349. else
  350. edma_clean_channel(davinci_spi_dma->dma_tx_channel);
  351. complete(&davinci_spi_dma->dma_tx_completion);
  352. /* We must disable the DMA TX request */
  353. davinci_spi_set_dma_req(spi, 0);
  354. }
  355. static int davinci_spi_request_dma(struct spi_device *spi)
  356. {
  357. struct davinci_spi *davinci_spi;
  358. struct davinci_spi_dma *davinci_spi_dma;
  359. struct device *sdev;
  360. int r;
  361. davinci_spi = spi_master_get_devdata(spi->master);
  362. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  363. sdev = davinci_spi->bitbang.master->dev.parent;
  364. r = edma_alloc_channel(davinci_spi_dma->dma_rx_sync_dev,
  365. davinci_spi_dma_rx_callback, spi,
  366. davinci_spi_dma->eventq);
  367. if (r < 0) {
  368. dev_dbg(sdev, "Unable to request DMA channel for SPI RX\n");
  369. return -EAGAIN;
  370. }
  371. davinci_spi_dma->dma_rx_channel = r;
  372. r = edma_alloc_channel(davinci_spi_dma->dma_tx_sync_dev,
  373. davinci_spi_dma_tx_callback, spi,
  374. davinci_spi_dma->eventq);
  375. if (r < 0) {
  376. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  377. davinci_spi_dma->dma_rx_channel = -1;
  378. dev_dbg(sdev, "Unable to request DMA channel for SPI TX\n");
  379. return -EAGAIN;
  380. }
  381. davinci_spi_dma->dma_tx_channel = r;
  382. return 0;
  383. }
  384. /**
  385. * davinci_spi_setup - This functions will set default transfer method
  386. * @spi: spi device on which data transfer to be done
  387. *
  388. * This functions sets the default transfer method.
  389. */
  390. static int davinci_spi_setup(struct spi_device *spi)
  391. {
  392. int retval;
  393. struct davinci_spi *davinci_spi;
  394. struct davinci_spi_dma *davinci_spi_dma;
  395. davinci_spi = spi_master_get_devdata(spi->master);
  396. /* if bits per word length is zero then set it default 8 */
  397. if (!spi->bits_per_word)
  398. spi->bits_per_word = 8;
  399. if (use_dma && davinci_spi->dma_channels) {
  400. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  401. if ((davinci_spi_dma->dma_rx_channel == -1)
  402. || (davinci_spi_dma->dma_tx_channel == -1)) {
  403. retval = davinci_spi_request_dma(spi);
  404. if (retval < 0)
  405. return retval;
  406. }
  407. }
  408. retval = davinci_spi_setup_transfer(spi, NULL);
  409. return retval;
  410. }
  411. static void davinci_spi_cleanup(struct spi_device *spi)
  412. {
  413. struct davinci_spi *davinci_spi = spi_master_get_devdata(spi->master);
  414. struct davinci_spi_dma *davinci_spi_dma;
  415. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  416. if (use_dma && davinci_spi->dma_channels) {
  417. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  418. if ((davinci_spi_dma->dma_rx_channel != -1)
  419. && (davinci_spi_dma->dma_tx_channel != -1)) {
  420. edma_free_channel(davinci_spi_dma->dma_tx_channel);
  421. edma_free_channel(davinci_spi_dma->dma_rx_channel);
  422. }
  423. }
  424. }
  425. static int davinci_spi_bufs_prep(struct spi_device *spi,
  426. struct davinci_spi *davinci_spi)
  427. {
  428. struct davinci_spi_platform_data *pdata;
  429. int op_mode = 0;
  430. /*
  431. * REVISIT unless devices disagree about SPI_LOOP or
  432. * SPI_READY (SPI_NO_CS only allows one device!), this
  433. * should not need to be done before each message...
  434. * optimize for both flags staying cleared.
  435. */
  436. op_mode = SPIPC0_DIFUN_MASK
  437. | SPIPC0_DOFUN_MASK
  438. | SPIPC0_CLKFUN_MASK;
  439. if (!(spi->mode & SPI_NO_CS)) {
  440. pdata = davinci_spi->pdata;
  441. if (!pdata->chip_sel ||
  442. pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)
  443. op_mode |= 1 << spi->chip_select;
  444. }
  445. if (spi->mode & SPI_READY)
  446. op_mode |= SPIPC0_SPIENA_MASK;
  447. iowrite32(op_mode, davinci_spi->base + SPIPC0);
  448. if (spi->mode & SPI_LOOP)
  449. set_io_bits(davinci_spi->base + SPIGCR1,
  450. SPIGCR1_LOOPBACK_MASK);
  451. else
  452. clear_io_bits(davinci_spi->base + SPIGCR1,
  453. SPIGCR1_LOOPBACK_MASK);
  454. return 0;
  455. }
  456. static int davinci_spi_check_error(struct davinci_spi *davinci_spi,
  457. int int_status)
  458. {
  459. struct device *sdev = davinci_spi->bitbang.master->dev.parent;
  460. if (int_status & SPIFLG_TIMEOUT_MASK) {
  461. dev_dbg(sdev, "SPI Time-out Error\n");
  462. return -ETIMEDOUT;
  463. }
  464. if (int_status & SPIFLG_DESYNC_MASK) {
  465. dev_dbg(sdev, "SPI Desynchronization Error\n");
  466. return -EIO;
  467. }
  468. if (int_status & SPIFLG_BITERR_MASK) {
  469. dev_dbg(sdev, "SPI Bit error\n");
  470. return -EIO;
  471. }
  472. if (davinci_spi->version == SPI_VERSION_2) {
  473. if (int_status & SPIFLG_DLEN_ERR_MASK) {
  474. dev_dbg(sdev, "SPI Data Length Error\n");
  475. return -EIO;
  476. }
  477. if (int_status & SPIFLG_PARERR_MASK) {
  478. dev_dbg(sdev, "SPI Parity Error\n");
  479. return -EIO;
  480. }
  481. if (int_status & SPIFLG_OVRRUN_MASK) {
  482. dev_dbg(sdev, "SPI Data Overrun error\n");
  483. return -EIO;
  484. }
  485. if (int_status & SPIFLG_TX_INTR_MASK) {
  486. dev_dbg(sdev, "SPI TX intr bit set\n");
  487. return -EIO;
  488. }
  489. if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) {
  490. dev_dbg(sdev, "SPI Buffer Init Active\n");
  491. return -EBUSY;
  492. }
  493. }
  494. return 0;
  495. }
  496. /**
  497. * davinci_spi_bufs - functions which will handle transfer data
  498. * @spi: spi device on which data transfer to be done
  499. * @t: spi transfer in which transfer info is filled
  500. *
  501. * This function will put data to be transferred into data register
  502. * of SPI controller and then wait until the completion will be marked
  503. * by the IRQ Handler.
  504. */
  505. static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t)
  506. {
  507. struct davinci_spi *davinci_spi;
  508. int int_status, count, ret;
  509. u8 conv;
  510. u32 tx_data, data1_reg_val;
  511. u32 buf_val, flg_val;
  512. struct davinci_spi_platform_data *pdata;
  513. davinci_spi = spi_master_get_devdata(spi->master);
  514. pdata = davinci_spi->pdata;
  515. davinci_spi->tx = t->tx_buf;
  516. davinci_spi->rx = t->rx_buf;
  517. /* convert len to words based on bits_per_word */
  518. conv = davinci_spi->bytes_per_word[spi->chip_select];
  519. davinci_spi->count = t->len / conv;
  520. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  521. INIT_COMPLETION(davinci_spi->done);
  522. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  523. if (ret)
  524. return ret;
  525. /* Enable SPI */
  526. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  527. count = davinci_spi->count;
  528. /* Determine the command to execute READ or WRITE */
  529. if (t->tx_buf) {
  530. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  531. while (1) {
  532. tx_data = davinci_spi->get_tx(davinci_spi);
  533. data1_reg_val &= ~(0xFFFF);
  534. data1_reg_val |= (0xFFFF & tx_data);
  535. buf_val = ioread32(davinci_spi->base + SPIBUF);
  536. if ((buf_val & SPIBUF_TXFULL_MASK) == 0) {
  537. iowrite32(data1_reg_val,
  538. davinci_spi->base + SPIDAT1);
  539. count--;
  540. }
  541. while (ioread32(davinci_spi->base + SPIBUF)
  542. & SPIBUF_RXEMPTY_MASK)
  543. cpu_relax();
  544. /* getting the returned byte */
  545. if (t->rx_buf) {
  546. buf_val = ioread32(davinci_spi->base + SPIBUF);
  547. davinci_spi->get_rx(buf_val, davinci_spi);
  548. }
  549. if (count <= 0)
  550. break;
  551. }
  552. } else {
  553. if (pdata->poll_mode) {
  554. while (1) {
  555. /* keeps the serial clock going */
  556. if ((ioread32(davinci_spi->base + SPIBUF)
  557. & SPIBUF_TXFULL_MASK) == 0)
  558. iowrite32(data1_reg_val,
  559. davinci_spi->base + SPIDAT1);
  560. while (ioread32(davinci_spi->base + SPIBUF) &
  561. SPIBUF_RXEMPTY_MASK)
  562. cpu_relax();
  563. flg_val = ioread32(davinci_spi->base + SPIFLG);
  564. buf_val = ioread32(davinci_spi->base + SPIBUF);
  565. davinci_spi->get_rx(buf_val, davinci_spi);
  566. count--;
  567. if (count <= 0)
  568. break;
  569. }
  570. } else { /* Receive in Interrupt mode */
  571. int i;
  572. for (i = 0; i < davinci_spi->count; i++) {
  573. set_io_bits(davinci_spi->base + SPIINT,
  574. SPIINT_BITERR_INTR
  575. | SPIINT_OVRRUN_INTR
  576. | SPIINT_RX_INTR);
  577. iowrite32(data1_reg_val,
  578. davinci_spi->base + SPIDAT1);
  579. while (ioread32(davinci_spi->base + SPIINT) &
  580. SPIINT_RX_INTR)
  581. cpu_relax();
  582. }
  583. iowrite32((data1_reg_val & 0x0ffcffff),
  584. davinci_spi->base + SPIDAT1);
  585. }
  586. }
  587. /*
  588. * Check for bit error, desync error,parity error,timeout error and
  589. * receive overflow errors
  590. */
  591. int_status = ioread32(davinci_spi->base + SPIFLG);
  592. ret = davinci_spi_check_error(davinci_spi, int_status);
  593. if (ret != 0)
  594. return ret;
  595. return t->len;
  596. }
  597. static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t)
  598. {
  599. struct davinci_spi *davinci_spi;
  600. int int_status = 0;
  601. int count, temp_count;
  602. u32 data1_reg_val;
  603. struct davinci_spi_dma *davinci_spi_dma;
  604. int data_type, ret;
  605. unsigned long tx_reg, rx_reg;
  606. struct device *sdev;
  607. davinci_spi = spi_master_get_devdata(spi->master);
  608. sdev = davinci_spi->bitbang.master->dev.parent;
  609. davinci_spi_dma = &davinci_spi->dma_channels[spi->chip_select];
  610. tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1;
  611. rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF;
  612. davinci_spi->tx = t->tx_buf;
  613. davinci_spi->rx = t->rx_buf;
  614. /* convert len to words based on bits_per_word */
  615. data_type = davinci_spi->bytes_per_word[spi->chip_select];
  616. davinci_spi->count = t->len / data_type;
  617. data1_reg_val = ioread32(davinci_spi->base + SPIDAT1);
  618. INIT_COMPLETION(davinci_spi->done);
  619. init_completion(&davinci_spi_dma->dma_rx_completion);
  620. init_completion(&davinci_spi_dma->dma_tx_completion);
  621. ret = davinci_spi_bufs_prep(spi, davinci_spi);
  622. if (ret)
  623. return ret;
  624. count = davinci_spi->count; /* the number of elements */
  625. /* disable all interrupts for dma transfers */
  626. clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL);
  627. /* Disable SPI to write configuration bits in SPIDAT */
  628. clear_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  629. /* Enable SPI */
  630. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK);
  631. if (t->tx_buf) {
  632. t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count,
  633. DMA_TO_DEVICE);
  634. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  635. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  636. " TX buffer\n", count);
  637. return -ENOMEM;
  638. }
  639. temp_count = count;
  640. } else {
  641. /* We need TX clocking for RX transaction */
  642. t->tx_dma = dma_map_single(&spi->dev,
  643. (void *)davinci_spi->tmp_buf, count + 1,
  644. DMA_TO_DEVICE);
  645. if (dma_mapping_error(&spi->dev, t->tx_dma)) {
  646. dev_dbg(sdev, "Unable to DMA map a %d bytes"
  647. " TX tmp buffer\n", count);
  648. return -ENOMEM;
  649. }
  650. temp_count = count + 1;
  651. }
  652. edma_set_transfer_params(davinci_spi_dma->dma_tx_channel,
  653. data_type, temp_count, 1, 0, ASYNC);
  654. edma_set_dest(davinci_spi_dma->dma_tx_channel, tx_reg, INCR, W8BIT);
  655. edma_set_src(davinci_spi_dma->dma_tx_channel, t->tx_dma, INCR, W8BIT);
  656. edma_set_src_index(davinci_spi_dma->dma_tx_channel, data_type, 0);
  657. edma_set_dest_index(davinci_spi_dma->dma_tx_channel, 0, 0);
  658. if (t->rx_buf) {
  659. /* initiate transaction */
  660. iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1);
  661. t->rx_dma = dma_map_single(&spi->dev, (void *)t->rx_buf, count,
  662. DMA_FROM_DEVICE);
  663. if (dma_mapping_error(&spi->dev, t->rx_dma)) {
  664. dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n",
  665. count);
  666. if (t->tx_buf != NULL)
  667. dma_unmap_single(NULL, t->tx_dma,
  668. count, DMA_TO_DEVICE);
  669. return -ENOMEM;
  670. }
  671. edma_set_transfer_params(davinci_spi_dma->dma_rx_channel,
  672. data_type, count, 1, 0, ASYNC);
  673. edma_set_src(davinci_spi_dma->dma_rx_channel,
  674. rx_reg, INCR, W8BIT);
  675. edma_set_dest(davinci_spi_dma->dma_rx_channel,
  676. t->rx_dma, INCR, W8BIT);
  677. edma_set_src_index(davinci_spi_dma->dma_rx_channel, 0, 0);
  678. edma_set_dest_index(davinci_spi_dma->dma_rx_channel,
  679. data_type, 0);
  680. }
  681. if ((t->tx_buf) || (t->rx_buf))
  682. edma_start(davinci_spi_dma->dma_tx_channel);
  683. if (t->rx_buf)
  684. edma_start(davinci_spi_dma->dma_rx_channel);
  685. if ((t->rx_buf) || (t->tx_buf))
  686. davinci_spi_set_dma_req(spi, 1);
  687. if (t->tx_buf)
  688. wait_for_completion_interruptible(
  689. &davinci_spi_dma->dma_tx_completion);
  690. if (t->rx_buf)
  691. wait_for_completion_interruptible(
  692. &davinci_spi_dma->dma_rx_completion);
  693. dma_unmap_single(NULL, t->tx_dma, temp_count, DMA_TO_DEVICE);
  694. if (t->rx_buf)
  695. dma_unmap_single(NULL, t->rx_dma, count, DMA_FROM_DEVICE);
  696. /*
  697. * Check for bit error, desync error,parity error,timeout error and
  698. * receive overflow errors
  699. */
  700. int_status = ioread32(davinci_spi->base + SPIFLG);
  701. ret = davinci_spi_check_error(davinci_spi, int_status);
  702. if (ret != 0)
  703. return ret;
  704. return t->len;
  705. }
  706. /**
  707. * davinci_spi_irq - IRQ handler for DaVinci SPI
  708. * @irq: IRQ number for this SPI Master
  709. * @context_data: structure for SPI Master controller davinci_spi
  710. */
  711. static irqreturn_t davinci_spi_irq(s32 irq, void *context_data)
  712. {
  713. struct davinci_spi *davinci_spi = context_data;
  714. u32 int_status, rx_data = 0;
  715. irqreturn_t ret = IRQ_NONE;
  716. int_status = ioread32(davinci_spi->base + SPIFLG);
  717. while ((int_status & SPIFLG_RX_INTR_MASK)) {
  718. if (likely(int_status & SPIFLG_RX_INTR_MASK)) {
  719. ret = IRQ_HANDLED;
  720. rx_data = ioread32(davinci_spi->base + SPIBUF);
  721. davinci_spi->get_rx(rx_data, davinci_spi);
  722. /* Disable Receive Interrupt */
  723. iowrite32(~(SPIINT_RX_INTR | SPIINT_TX_INTR),
  724. davinci_spi->base + SPIINT);
  725. } else
  726. (void)davinci_spi_check_error(davinci_spi, int_status);
  727. int_status = ioread32(davinci_spi->base + SPIFLG);
  728. }
  729. return ret;
  730. }
  731. /**
  732. * davinci_spi_probe - probe function for SPI Master Controller
  733. * @pdev: platform_device structure which contains plateform specific data
  734. */
  735. static int davinci_spi_probe(struct platform_device *pdev)
  736. {
  737. struct spi_master *master;
  738. struct davinci_spi *davinci_spi;
  739. struct davinci_spi_platform_data *pdata;
  740. struct resource *r, *mem;
  741. resource_size_t dma_rx_chan = SPI_NO_RESOURCE;
  742. resource_size_t dma_tx_chan = SPI_NO_RESOURCE;
  743. resource_size_t dma_eventq = SPI_NO_RESOURCE;
  744. int i = 0, ret = 0;
  745. pdata = pdev->dev.platform_data;
  746. if (pdata == NULL) {
  747. ret = -ENODEV;
  748. goto err;
  749. }
  750. master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi));
  751. if (master == NULL) {
  752. ret = -ENOMEM;
  753. goto err;
  754. }
  755. dev_set_drvdata(&pdev->dev, master);
  756. davinci_spi = spi_master_get_devdata(master);
  757. if (davinci_spi == NULL) {
  758. ret = -ENOENT;
  759. goto free_master;
  760. }
  761. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  762. if (r == NULL) {
  763. ret = -ENOENT;
  764. goto free_master;
  765. }
  766. davinci_spi->pbase = r->start;
  767. davinci_spi->region_size = resource_size(r);
  768. davinci_spi->pdata = pdata;
  769. mem = request_mem_region(r->start, davinci_spi->region_size,
  770. pdev->name);
  771. if (mem == NULL) {
  772. ret = -EBUSY;
  773. goto free_master;
  774. }
  775. davinci_spi->base = ioremap(r->start, davinci_spi->region_size);
  776. if (davinci_spi->base == NULL) {
  777. ret = -ENOMEM;
  778. goto release_region;
  779. }
  780. davinci_spi->irq = platform_get_irq(pdev, 0);
  781. if (davinci_spi->irq <= 0) {
  782. ret = -EINVAL;
  783. goto unmap_io;
  784. }
  785. ret = request_irq(davinci_spi->irq, davinci_spi_irq, IRQF_DISABLED,
  786. dev_name(&pdev->dev), davinci_spi);
  787. if (ret)
  788. goto unmap_io;
  789. /* Allocate tmp_buf for tx_buf */
  790. davinci_spi->tmp_buf = kzalloc(SPI_BUFSIZ, GFP_KERNEL);
  791. if (davinci_spi->tmp_buf == NULL) {
  792. ret = -ENOMEM;
  793. goto irq_free;
  794. }
  795. davinci_spi->bitbang.master = spi_master_get(master);
  796. if (davinci_spi->bitbang.master == NULL) {
  797. ret = -ENODEV;
  798. goto free_tmp_buf;
  799. }
  800. davinci_spi->clk = clk_get(&pdev->dev, NULL);
  801. if (IS_ERR(davinci_spi->clk)) {
  802. ret = -ENODEV;
  803. goto put_master;
  804. }
  805. clk_enable(davinci_spi->clk);
  806. master->bus_num = pdev->id;
  807. master->num_chipselect = pdata->num_chipselect;
  808. master->setup = davinci_spi_setup;
  809. master->cleanup = davinci_spi_cleanup;
  810. davinci_spi->bitbang.chipselect = davinci_spi_chipselect;
  811. davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer;
  812. davinci_spi->version = pdata->version;
  813. use_dma = pdata->use_dma;
  814. davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP;
  815. if (davinci_spi->version == SPI_VERSION_2)
  816. davinci_spi->bitbang.flags |= SPI_READY;
  817. if (use_dma) {
  818. r = platform_get_resource(pdev, IORESOURCE_DMA, 0);
  819. if (r)
  820. dma_rx_chan = r->start;
  821. r = platform_get_resource(pdev, IORESOURCE_DMA, 1);
  822. if (r)
  823. dma_tx_chan = r->start;
  824. r = platform_get_resource(pdev, IORESOURCE_DMA, 2);
  825. if (r)
  826. dma_eventq = r->start;
  827. }
  828. if (!use_dma ||
  829. dma_rx_chan == SPI_NO_RESOURCE ||
  830. dma_tx_chan == SPI_NO_RESOURCE ||
  831. dma_eventq == SPI_NO_RESOURCE) {
  832. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio;
  833. use_dma = 0;
  834. } else {
  835. davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma;
  836. davinci_spi->dma_channels = kzalloc(master->num_chipselect
  837. * sizeof(struct davinci_spi_dma), GFP_KERNEL);
  838. if (davinci_spi->dma_channels == NULL) {
  839. ret = -ENOMEM;
  840. goto free_clk;
  841. }
  842. for (i = 0; i < master->num_chipselect; i++) {
  843. davinci_spi->dma_channels[i].dma_rx_channel = -1;
  844. davinci_spi->dma_channels[i].dma_rx_sync_dev =
  845. dma_rx_chan;
  846. davinci_spi->dma_channels[i].dma_tx_channel = -1;
  847. davinci_spi->dma_channels[i].dma_tx_sync_dev =
  848. dma_tx_chan;
  849. davinci_spi->dma_channels[i].eventq = dma_eventq;
  850. }
  851. dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n"
  852. "Using RX channel = %d , TX channel = %d and "
  853. "event queue = %d", dma_rx_chan, dma_tx_chan,
  854. dma_eventq);
  855. }
  856. davinci_spi->get_rx = davinci_spi_rx_buf_u8;
  857. davinci_spi->get_tx = davinci_spi_tx_buf_u8;
  858. init_completion(&davinci_spi->done);
  859. /* Reset In/OUT SPI module */
  860. iowrite32(0, davinci_spi->base + SPIGCR0);
  861. udelay(100);
  862. iowrite32(1, davinci_spi->base + SPIGCR0);
  863. /* initialize chip selects */
  864. if (pdata->chip_sel) {
  865. for (i = 0; i < pdata->num_chipselect; i++) {
  866. if (pdata->chip_sel[i] != SPI_INTERN_CS)
  867. gpio_direction_output(pdata->chip_sel[i], 1);
  868. }
  869. }
  870. /* Clock internal */
  871. if (davinci_spi->pdata->clk_internal)
  872. set_io_bits(davinci_spi->base + SPIGCR1,
  873. SPIGCR1_CLKMOD_MASK);
  874. else
  875. clear_io_bits(davinci_spi->base + SPIGCR1,
  876. SPIGCR1_CLKMOD_MASK);
  877. iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF);
  878. /* master mode default */
  879. set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK);
  880. if (davinci_spi->pdata->intr_level)
  881. iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL);
  882. else
  883. iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL);
  884. ret = spi_bitbang_start(&davinci_spi->bitbang);
  885. if (ret)
  886. goto free_clk;
  887. dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base);
  888. if (!pdata->poll_mode)
  889. dev_info(&pdev->dev, "Operating in interrupt mode"
  890. " using IRQ %d\n", davinci_spi->irq);
  891. return ret;
  892. free_clk:
  893. clk_disable(davinci_spi->clk);
  894. clk_put(davinci_spi->clk);
  895. put_master:
  896. spi_master_put(master);
  897. free_tmp_buf:
  898. kfree(davinci_spi->tmp_buf);
  899. irq_free:
  900. free_irq(davinci_spi->irq, davinci_spi);
  901. unmap_io:
  902. iounmap(davinci_spi->base);
  903. release_region:
  904. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  905. free_master:
  906. kfree(master);
  907. err:
  908. return ret;
  909. }
  910. /**
  911. * davinci_spi_remove - remove function for SPI Master Controller
  912. * @pdev: platform_device structure which contains plateform specific data
  913. *
  914. * This function will do the reverse action of davinci_spi_probe function
  915. * It will free the IRQ and SPI controller's memory region.
  916. * It will also call spi_bitbang_stop to destroy the work queue which was
  917. * created by spi_bitbang_start.
  918. */
  919. static int __exit davinci_spi_remove(struct platform_device *pdev)
  920. {
  921. struct davinci_spi *davinci_spi;
  922. struct spi_master *master;
  923. master = dev_get_drvdata(&pdev->dev);
  924. davinci_spi = spi_master_get_devdata(master);
  925. spi_bitbang_stop(&davinci_spi->bitbang);
  926. clk_disable(davinci_spi->clk);
  927. clk_put(davinci_spi->clk);
  928. spi_master_put(master);
  929. kfree(davinci_spi->tmp_buf);
  930. free_irq(davinci_spi->irq, davinci_spi);
  931. iounmap(davinci_spi->base);
  932. release_mem_region(davinci_spi->pbase, davinci_spi->region_size);
  933. return 0;
  934. }
  935. static struct platform_driver davinci_spi_driver = {
  936. .driver.name = "spi_davinci",
  937. .remove = __exit_p(davinci_spi_remove),
  938. };
  939. static int __init davinci_spi_init(void)
  940. {
  941. return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe);
  942. }
  943. module_init(davinci_spi_init);
  944. static void __exit davinci_spi_exit(void)
  945. {
  946. platform_driver_unregister(&davinci_spi_driver);
  947. }
  948. module_exit(davinci_spi_exit);
  949. MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver");
  950. MODULE_LICENSE("GPL");