spi-tegra.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Driver for Nvidia TEGRA spi controller.
  3. *
  4. * Copyright (C) 2010 Google, Inc.
  5. *
  6. * Author:
  7. * Erik Gilling <konkers@android.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/err.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/dmapool.h>
  27. #include <linux/clk.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/delay.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/dmaengine.h>
  32. #include <mach/dma.h>
  33. #define SLINK_COMMAND 0x000
  34. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  35. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  36. #define SLINK_BOTH_EN (1 << 10)
  37. #define SLINK_CS_SW (1 << 11)
  38. #define SLINK_CS_VALUE (1 << 12)
  39. #define SLINK_CS_POLARITY (1 << 13)
  40. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  41. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  42. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  43. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  44. #define SLINK_IDLE_SDA_MASK (3 << 16)
  45. #define SLINK_CS_POLARITY1 (1 << 20)
  46. #define SLINK_CK_SDA (1 << 21)
  47. #define SLINK_CS_POLARITY2 (1 << 22)
  48. #define SLINK_CS_POLARITY3 (1 << 23)
  49. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  50. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  51. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  52. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  53. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  54. #define SLINK_M_S (1 << 28)
  55. #define SLINK_WAIT (1 << 29)
  56. #define SLINK_GO (1 << 30)
  57. #define SLINK_ENB (1 << 31)
  58. #define SLINK_COMMAND2 0x004
  59. #define SLINK_LSBFE (1 << 0)
  60. #define SLINK_SSOE (1 << 1)
  61. #define SLINK_SPIE (1 << 4)
  62. #define SLINK_BIDIROE (1 << 6)
  63. #define SLINK_MODFEN (1 << 7)
  64. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  65. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  66. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  67. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  68. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  69. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  70. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  71. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  72. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  73. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  74. #define SLINK_SPC0 (1 << 29)
  75. #define SLINK_TXEN (1 << 30)
  76. #define SLINK_RXEN (1 << 31)
  77. #define SLINK_STATUS 0x008
  78. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  79. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  80. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  81. #define SLINK_MODF (1 << 16)
  82. #define SLINK_RX_UNF (1 << 18)
  83. #define SLINK_TX_OVF (1 << 19)
  84. #define SLINK_TX_FULL (1 << 20)
  85. #define SLINK_TX_EMPTY (1 << 21)
  86. #define SLINK_RX_FULL (1 << 22)
  87. #define SLINK_RX_EMPTY (1 << 23)
  88. #define SLINK_TX_UNF (1 << 24)
  89. #define SLINK_RX_OVF (1 << 25)
  90. #define SLINK_TX_FLUSH (1 << 26)
  91. #define SLINK_RX_FLUSH (1 << 27)
  92. #define SLINK_SCLK (1 << 28)
  93. #define SLINK_ERR (1 << 29)
  94. #define SLINK_RDY (1 << 30)
  95. #define SLINK_BSY (1 << 31)
  96. #define SLINK_MAS_DATA 0x010
  97. #define SLINK_SLAVE_DATA 0x014
  98. #define SLINK_DMA_CTL 0x018
  99. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  100. #define SLINK_TX_TRIG_1 (0 << 16)
  101. #define SLINK_TX_TRIG_4 (1 << 16)
  102. #define SLINK_TX_TRIG_8 (2 << 16)
  103. #define SLINK_TX_TRIG_16 (3 << 16)
  104. #define SLINK_TX_TRIG_MASK (3 << 16)
  105. #define SLINK_RX_TRIG_1 (0 << 18)
  106. #define SLINK_RX_TRIG_4 (1 << 18)
  107. #define SLINK_RX_TRIG_8 (2 << 18)
  108. #define SLINK_RX_TRIG_16 (3 << 18)
  109. #define SLINK_RX_TRIG_MASK (3 << 18)
  110. #define SLINK_PACKED (1 << 20)
  111. #define SLINK_PACK_SIZE_4 (0 << 21)
  112. #define SLINK_PACK_SIZE_8 (1 << 21)
  113. #define SLINK_PACK_SIZE_16 (2 << 21)
  114. #define SLINK_PACK_SIZE_32 (3 << 21)
  115. #define SLINK_PACK_SIZE_MASK (3 << 21)
  116. #define SLINK_IE_TXC (1 << 26)
  117. #define SLINK_IE_RXC (1 << 27)
  118. #define SLINK_DMA_EN (1 << 31)
  119. #define SLINK_STATUS2 0x01c
  120. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  121. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f) >> 16)
  122. #define SLINK_TX_FIFO 0x100
  123. #define SLINK_RX_FIFO 0x180
  124. static const unsigned long spi_tegra_req_sels[] = {
  125. TEGRA_DMA_REQ_SEL_SL2B1,
  126. TEGRA_DMA_REQ_SEL_SL2B2,
  127. TEGRA_DMA_REQ_SEL_SL2B3,
  128. TEGRA_DMA_REQ_SEL_SL2B4,
  129. };
  130. #define BB_LEN 32
  131. struct spi_tegra_data {
  132. struct spi_master *master;
  133. struct platform_device *pdev;
  134. spinlock_t lock;
  135. struct clk *clk;
  136. void __iomem *base;
  137. unsigned long phys;
  138. u32 cur_speed;
  139. struct list_head queue;
  140. struct spi_transfer *cur;
  141. unsigned cur_pos;
  142. unsigned cur_len;
  143. unsigned cur_bytes_per_word;
  144. /* The tegra spi controller has a bug which causes the first word
  145. * in PIO transactions to be garbage. Since packed DMA transactions
  146. * require transfers to be 4 byte aligned we need a bounce buffer
  147. * for the generic case.
  148. */
  149. int dma_req_len;
  150. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  151. struct tegra_dma_req rx_dma_req;
  152. struct tegra_dma_channel *rx_dma;
  153. #else
  154. struct dma_chan *rx_dma;
  155. struct dma_slave_config sconfig;
  156. struct dma_async_tx_descriptor *rx_dma_desc;
  157. dma_cookie_t rx_cookie;
  158. #endif
  159. u32 *rx_bb;
  160. dma_addr_t rx_bb_phys;
  161. };
  162. #if !defined(CONFIG_TEGRA_SYSTEM_DMA)
  163. static void tegra_spi_rx_dma_complete(void *args);
  164. #endif
  165. static inline unsigned long spi_tegra_readl(struct spi_tegra_data *tspi,
  166. unsigned long reg)
  167. {
  168. return readl(tspi->base + reg);
  169. }
  170. static inline void spi_tegra_writel(struct spi_tegra_data *tspi,
  171. unsigned long val,
  172. unsigned long reg)
  173. {
  174. writel(val, tspi->base + reg);
  175. }
  176. static void spi_tegra_go(struct spi_tegra_data *tspi)
  177. {
  178. unsigned long val;
  179. wmb();
  180. val = spi_tegra_readl(tspi, SLINK_DMA_CTL);
  181. val &= ~SLINK_DMA_BLOCK_SIZE(~0) & ~SLINK_DMA_EN;
  182. val |= SLINK_DMA_BLOCK_SIZE(tspi->dma_req_len / 4 - 1);
  183. spi_tegra_writel(tspi, val, SLINK_DMA_CTL);
  184. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  185. tspi->rx_dma_req.size = tspi->dma_req_len;
  186. tegra_dma_enqueue_req(tspi->rx_dma, &tspi->rx_dma_req);
  187. #else
  188. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma,
  189. tspi->rx_bb_phys, tspi->dma_req_len,
  190. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  191. if (!tspi->rx_dma_desc) {
  192. dev_err(&tspi->pdev->dev, "dmaengine slave prep failed\n");
  193. return;
  194. }
  195. tspi->rx_dma_desc->callback = tegra_spi_rx_dma_complete;
  196. tspi->rx_dma_desc->callback_param = tspi;
  197. tspi->rx_cookie = dmaengine_submit(tspi->rx_dma_desc);
  198. dma_async_issue_pending(tspi->rx_dma);
  199. #endif
  200. val |= SLINK_DMA_EN;
  201. spi_tegra_writel(tspi, val, SLINK_DMA_CTL);
  202. }
  203. static unsigned spi_tegra_fill_tx_fifo(struct spi_tegra_data *tspi,
  204. struct spi_transfer *t)
  205. {
  206. unsigned len = min(t->len - tspi->cur_pos, BB_LEN *
  207. tspi->cur_bytes_per_word);
  208. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_pos;
  209. int i, j;
  210. unsigned long val;
  211. val = spi_tegra_readl(tspi, SLINK_COMMAND);
  212. val &= ~SLINK_WORD_SIZE(~0);
  213. val |= SLINK_WORD_SIZE(len / tspi->cur_bytes_per_word - 1);
  214. spi_tegra_writel(tspi, val, SLINK_COMMAND);
  215. for (i = 0; i < len; i += tspi->cur_bytes_per_word) {
  216. val = 0;
  217. for (j = 0; j < tspi->cur_bytes_per_word; j++)
  218. val |= tx_buf[i + j] << j * 8;
  219. spi_tegra_writel(tspi, val, SLINK_TX_FIFO);
  220. }
  221. tspi->dma_req_len = len / tspi->cur_bytes_per_word * 4;
  222. return len;
  223. }
  224. static unsigned spi_tegra_drain_rx_fifo(struct spi_tegra_data *tspi,
  225. struct spi_transfer *t)
  226. {
  227. unsigned len = tspi->cur_len;
  228. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_pos;
  229. int i, j;
  230. unsigned long val;
  231. for (i = 0; i < len; i += tspi->cur_bytes_per_word) {
  232. val = tspi->rx_bb[i / tspi->cur_bytes_per_word];
  233. for (j = 0; j < tspi->cur_bytes_per_word; j++)
  234. rx_buf[i + j] = (val >> (j * 8)) & 0xff;
  235. }
  236. return len;
  237. }
  238. static void spi_tegra_start_transfer(struct spi_device *spi,
  239. struct spi_transfer *t)
  240. {
  241. struct spi_tegra_data *tspi = spi_master_get_devdata(spi->master);
  242. u32 speed;
  243. u8 bits_per_word;
  244. unsigned long val;
  245. speed = t->speed_hz ? t->speed_hz : spi->max_speed_hz;
  246. bits_per_word = t->bits_per_word ? t->bits_per_word :
  247. spi->bits_per_word;
  248. tspi->cur_bytes_per_word = (bits_per_word - 1) / 8 + 1;
  249. if (speed != tspi->cur_speed)
  250. clk_set_rate(tspi->clk, speed);
  251. if (tspi->cur_speed == 0)
  252. clk_prepare_enable(tspi->clk);
  253. tspi->cur_speed = speed;
  254. val = spi_tegra_readl(tspi, SLINK_COMMAND2);
  255. val &= ~SLINK_SS_EN_CS(~0) | SLINK_RXEN | SLINK_TXEN;
  256. if (t->rx_buf)
  257. val |= SLINK_RXEN;
  258. if (t->tx_buf)
  259. val |= SLINK_TXEN;
  260. val |= SLINK_SS_EN_CS(spi->chip_select);
  261. val |= SLINK_SPIE;
  262. spi_tegra_writel(tspi, val, SLINK_COMMAND2);
  263. val = spi_tegra_readl(tspi, SLINK_COMMAND);
  264. val &= ~SLINK_BIT_LENGTH(~0);
  265. val |= SLINK_BIT_LENGTH(bits_per_word - 1);
  266. /* FIXME: should probably control CS manually so that we can be sure
  267. * it does not go low between transfer and to support delay_usecs
  268. * correctly.
  269. */
  270. val &= ~SLINK_IDLE_SCLK_MASK & ~SLINK_CK_SDA & ~SLINK_CS_SW;
  271. if (spi->mode & SPI_CPHA)
  272. val |= SLINK_CK_SDA;
  273. if (spi->mode & SPI_CPOL)
  274. val |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  275. else
  276. val |= SLINK_IDLE_SCLK_DRIVE_LOW;
  277. val |= SLINK_M_S;
  278. spi_tegra_writel(tspi, val, SLINK_COMMAND);
  279. spi_tegra_writel(tspi, SLINK_RX_FLUSH | SLINK_TX_FLUSH, SLINK_STATUS);
  280. tspi->cur = t;
  281. tspi->cur_pos = 0;
  282. tspi->cur_len = spi_tegra_fill_tx_fifo(tspi, t);
  283. spi_tegra_go(tspi);
  284. }
  285. static void spi_tegra_start_message(struct spi_device *spi,
  286. struct spi_message *m)
  287. {
  288. struct spi_transfer *t;
  289. m->actual_length = 0;
  290. m->status = 0;
  291. t = list_first_entry(&m->transfers, struct spi_transfer, transfer_list);
  292. spi_tegra_start_transfer(spi, t);
  293. }
  294. static void handle_spi_rx_dma_complete(struct spi_tegra_data *tspi)
  295. {
  296. unsigned long flags;
  297. struct spi_message *m;
  298. struct spi_device *spi;
  299. int timeout = 0;
  300. unsigned long val;
  301. /* the SPI controller may come back with both the BSY and RDY bits
  302. * set. In this case we need to wait for the BSY bit to clear so
  303. * that we are sure the DMA is finished. 1000 reads was empirically
  304. * determined to be long enough.
  305. */
  306. while (timeout++ < 1000) {
  307. if (!(spi_tegra_readl(tspi, SLINK_STATUS) & SLINK_BSY))
  308. break;
  309. }
  310. spin_lock_irqsave(&tspi->lock, flags);
  311. val = spi_tegra_readl(tspi, SLINK_STATUS);
  312. val |= SLINK_RDY;
  313. spi_tegra_writel(tspi, val, SLINK_STATUS);
  314. m = list_first_entry(&tspi->queue, struct spi_message, queue);
  315. if (timeout >= 1000)
  316. m->status = -EIO;
  317. spi = m->state;
  318. tspi->cur_pos += spi_tegra_drain_rx_fifo(tspi, tspi->cur);
  319. m->actual_length += tspi->cur_pos;
  320. if (tspi->cur_pos < tspi->cur->len) {
  321. tspi->cur_len = spi_tegra_fill_tx_fifo(tspi, tspi->cur);
  322. spi_tegra_go(tspi);
  323. } else if (!list_is_last(&tspi->cur->transfer_list,
  324. &m->transfers)) {
  325. tspi->cur = list_first_entry(&tspi->cur->transfer_list,
  326. struct spi_transfer,
  327. transfer_list);
  328. spi_tegra_start_transfer(spi, tspi->cur);
  329. } else {
  330. list_del(&m->queue);
  331. m->complete(m->context);
  332. if (!list_empty(&tspi->queue)) {
  333. m = list_first_entry(&tspi->queue, struct spi_message,
  334. queue);
  335. spi = m->state;
  336. spi_tegra_start_message(spi, m);
  337. } else {
  338. clk_disable_unprepare(tspi->clk);
  339. tspi->cur_speed = 0;
  340. }
  341. }
  342. spin_unlock_irqrestore(&tspi->lock, flags);
  343. }
  344. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  345. static void tegra_spi_rx_dma_complete(struct tegra_dma_req *req)
  346. {
  347. struct spi_tegra_data *tspi = req->dev;
  348. handle_spi_rx_dma_complete(tspi);
  349. }
  350. #else
  351. static void tegra_spi_rx_dma_complete(void *args)
  352. {
  353. struct spi_tegra_data *tspi = args;
  354. handle_spi_rx_dma_complete(tspi);
  355. }
  356. #endif
  357. static int spi_tegra_setup(struct spi_device *spi)
  358. {
  359. struct spi_tegra_data *tspi = spi_master_get_devdata(spi->master);
  360. unsigned long cs_bit;
  361. unsigned long val;
  362. unsigned long flags;
  363. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  364. spi->bits_per_word,
  365. spi->mode & SPI_CPOL ? "" : "~",
  366. spi->mode & SPI_CPHA ? "" : "~",
  367. spi->max_speed_hz);
  368. switch (spi->chip_select) {
  369. case 0:
  370. cs_bit = SLINK_CS_POLARITY;
  371. break;
  372. case 1:
  373. cs_bit = SLINK_CS_POLARITY1;
  374. break;
  375. case 2:
  376. cs_bit = SLINK_CS_POLARITY2;
  377. break;
  378. case 4:
  379. cs_bit = SLINK_CS_POLARITY3;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. spin_lock_irqsave(&tspi->lock, flags);
  385. val = spi_tegra_readl(tspi, SLINK_COMMAND);
  386. if (spi->mode & SPI_CS_HIGH)
  387. val |= cs_bit;
  388. else
  389. val &= ~cs_bit;
  390. spi_tegra_writel(tspi, val, SLINK_COMMAND);
  391. spin_unlock_irqrestore(&tspi->lock, flags);
  392. return 0;
  393. }
  394. static int spi_tegra_transfer(struct spi_device *spi, struct spi_message *m)
  395. {
  396. struct spi_tegra_data *tspi = spi_master_get_devdata(spi->master);
  397. struct spi_transfer *t;
  398. unsigned long flags;
  399. int was_empty;
  400. if (list_empty(&m->transfers) || !m->complete)
  401. return -EINVAL;
  402. list_for_each_entry(t, &m->transfers, transfer_list) {
  403. if (t->bits_per_word < 0 || t->bits_per_word > 32)
  404. return -EINVAL;
  405. if (t->len == 0)
  406. return -EINVAL;
  407. if (!t->rx_buf && !t->tx_buf)
  408. return -EINVAL;
  409. }
  410. m->state = spi;
  411. spin_lock_irqsave(&tspi->lock, flags);
  412. was_empty = list_empty(&tspi->queue);
  413. list_add_tail(&m->queue, &tspi->queue);
  414. if (was_empty)
  415. spi_tegra_start_message(spi, m);
  416. spin_unlock_irqrestore(&tspi->lock, flags);
  417. return 0;
  418. }
  419. static int __devinit spi_tegra_probe(struct platform_device *pdev)
  420. {
  421. struct spi_master *master;
  422. struct spi_tegra_data *tspi;
  423. struct resource *r;
  424. int ret;
  425. #if !defined(CONFIG_TEGRA_SYSTEM_DMA)
  426. dma_cap_mask_t mask;
  427. #endif
  428. master = spi_alloc_master(&pdev->dev, sizeof *tspi);
  429. if (master == NULL) {
  430. dev_err(&pdev->dev, "master allocation failed\n");
  431. return -ENOMEM;
  432. }
  433. /* the spi->mode bits understood by this driver: */
  434. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  435. master->bus_num = pdev->id;
  436. master->setup = spi_tegra_setup;
  437. master->transfer = spi_tegra_transfer;
  438. master->num_chipselect = 4;
  439. dev_set_drvdata(&pdev->dev, master);
  440. tspi = spi_master_get_devdata(master);
  441. tspi->master = master;
  442. tspi->pdev = pdev;
  443. spin_lock_init(&tspi->lock);
  444. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  445. if (r == NULL) {
  446. ret = -ENODEV;
  447. goto err0;
  448. }
  449. if (!request_mem_region(r->start, resource_size(r),
  450. dev_name(&pdev->dev))) {
  451. ret = -EBUSY;
  452. goto err0;
  453. }
  454. tspi->phys = r->start;
  455. tspi->base = ioremap(r->start, resource_size(r));
  456. if (!tspi->base) {
  457. dev_err(&pdev->dev, "can't ioremap iomem\n");
  458. ret = -ENOMEM;
  459. goto err1;
  460. }
  461. tspi->clk = clk_get(&pdev->dev, NULL);
  462. if (IS_ERR(tspi->clk)) {
  463. dev_err(&pdev->dev, "can not get clock\n");
  464. ret = PTR_ERR(tspi->clk);
  465. goto err2;
  466. }
  467. INIT_LIST_HEAD(&tspi->queue);
  468. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  469. tspi->rx_dma = tegra_dma_allocate_channel(TEGRA_DMA_MODE_ONESHOT);
  470. if (!tspi->rx_dma) {
  471. dev_err(&pdev->dev, "can not allocate rx dma channel\n");
  472. ret = -ENODEV;
  473. goto err3;
  474. }
  475. #else
  476. dma_cap_zero(mask);
  477. dma_cap_set(DMA_SLAVE, mask);
  478. tspi->rx_dma = dma_request_channel(mask, NULL, NULL);
  479. if (!tspi->rx_dma) {
  480. dev_err(&pdev->dev, "can not allocate rx dma channel\n");
  481. ret = -ENODEV;
  482. goto err3;
  483. }
  484. #endif
  485. tspi->rx_bb = dma_alloc_coherent(&pdev->dev, sizeof(u32) * BB_LEN,
  486. &tspi->rx_bb_phys, GFP_KERNEL);
  487. if (!tspi->rx_bb) {
  488. dev_err(&pdev->dev, "can not allocate rx bounce buffer\n");
  489. ret = -ENOMEM;
  490. goto err4;
  491. }
  492. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  493. tspi->rx_dma_req.complete = tegra_spi_rx_dma_complete;
  494. tspi->rx_dma_req.to_memory = 1;
  495. tspi->rx_dma_req.dest_addr = tspi->rx_bb_phys;
  496. tspi->rx_dma_req.dest_bus_width = 32;
  497. tspi->rx_dma_req.source_addr = tspi->phys + SLINK_RX_FIFO;
  498. tspi->rx_dma_req.source_bus_width = 32;
  499. tspi->rx_dma_req.source_wrap = 4;
  500. tspi->rx_dma_req.req_sel = spi_tegra_req_sels[pdev->id];
  501. tspi->rx_dma_req.dev = tspi;
  502. #else
  503. /* Dmaengine Dma slave config */
  504. tspi->sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  505. tspi->sconfig.dst_addr = tspi->phys + SLINK_RX_FIFO;
  506. tspi->sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  507. tspi->sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  508. tspi->sconfig.slave_id = spi_tegra_req_sels[pdev->id];
  509. tspi->sconfig.src_maxburst = 1;
  510. tspi->sconfig.dst_maxburst = 1;
  511. ret = dmaengine_device_control(tspi->rx_dma,
  512. DMA_SLAVE_CONFIG, (unsigned long) &tspi->sconfig);
  513. if (ret < 0) {
  514. dev_err(&pdev->dev, "can not do slave configure for dma %d\n",
  515. ret);
  516. goto err4;
  517. }
  518. #endif
  519. master->dev.of_node = pdev->dev.of_node;
  520. ret = spi_register_master(master);
  521. if (ret < 0)
  522. goto err5;
  523. return ret;
  524. err5:
  525. dma_free_coherent(&pdev->dev, sizeof(u32) * BB_LEN,
  526. tspi->rx_bb, tspi->rx_bb_phys);
  527. err4:
  528. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  529. tegra_dma_free_channel(tspi->rx_dma);
  530. #else
  531. dma_release_channel(tspi->rx_dma);
  532. #endif
  533. err3:
  534. clk_put(tspi->clk);
  535. err2:
  536. iounmap(tspi->base);
  537. err1:
  538. release_mem_region(r->start, resource_size(r));
  539. err0:
  540. spi_master_put(master);
  541. return ret;
  542. }
  543. static int __devexit spi_tegra_remove(struct platform_device *pdev)
  544. {
  545. struct spi_master *master;
  546. struct spi_tegra_data *tspi;
  547. struct resource *r;
  548. master = dev_get_drvdata(&pdev->dev);
  549. tspi = spi_master_get_devdata(master);
  550. spi_unregister_master(master);
  551. #if defined(CONFIG_TEGRA_SYSTEM_DMA)
  552. tegra_dma_free_channel(tspi->rx_dma);
  553. #else
  554. dma_release_channel(tspi->rx_dma);
  555. #endif
  556. dma_free_coherent(&pdev->dev, sizeof(u32) * BB_LEN,
  557. tspi->rx_bb, tspi->rx_bb_phys);
  558. clk_put(tspi->clk);
  559. iounmap(tspi->base);
  560. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  561. release_mem_region(r->start, resource_size(r));
  562. return 0;
  563. }
  564. MODULE_ALIAS("platform:spi_tegra");
  565. #ifdef CONFIG_OF
  566. static struct of_device_id spi_tegra_of_match_table[] __devinitdata = {
  567. { .compatible = "nvidia,tegra20-spi", },
  568. {}
  569. };
  570. MODULE_DEVICE_TABLE(of, spi_tegra_of_match_table);
  571. #else /* CONFIG_OF */
  572. #define spi_tegra_of_match_table NULL
  573. #endif /* CONFIG_OF */
  574. static struct platform_driver spi_tegra_driver = {
  575. .driver = {
  576. .name = "spi_tegra",
  577. .owner = THIS_MODULE,
  578. .of_match_table = spi_tegra_of_match_table,
  579. },
  580. .probe = spi_tegra_probe,
  581. .remove = __devexit_p(spi_tegra_remove),
  582. };
  583. module_platform_driver(spi_tegra_driver);
  584. MODULE_LICENSE("GPL");