davinci_spi.c 30 KB

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