spi-tegra20-slink.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * SPI driver for Nvidia's Tegra20/Tegra30 SLINK Controller.
  3. *
  4. * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/clk.h>
  19. #include <linux/completion.h>
  20. #include <linux/delay.h>
  21. #include <linux/dmaengine.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/dmapool.h>
  24. #include <linux/err.h>
  25. #include <linux/init.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/io.h>
  28. #include <linux/kernel.h>
  29. #include <linux/kthread.h>
  30. #include <linux/module.h>
  31. #include <linux/platform_device.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/of.h>
  34. #include <linux/of_device.h>
  35. #include <linux/reset.h>
  36. #include <linux/spi/spi.h>
  37. #define SLINK_COMMAND 0x000
  38. #define SLINK_BIT_LENGTH(x) (((x) & 0x1f) << 0)
  39. #define SLINK_WORD_SIZE(x) (((x) & 0x1f) << 5)
  40. #define SLINK_BOTH_EN (1 << 10)
  41. #define SLINK_CS_SW (1 << 11)
  42. #define SLINK_CS_VALUE (1 << 12)
  43. #define SLINK_CS_POLARITY (1 << 13)
  44. #define SLINK_IDLE_SDA_DRIVE_LOW (0 << 16)
  45. #define SLINK_IDLE_SDA_DRIVE_HIGH (1 << 16)
  46. #define SLINK_IDLE_SDA_PULL_LOW (2 << 16)
  47. #define SLINK_IDLE_SDA_PULL_HIGH (3 << 16)
  48. #define SLINK_IDLE_SDA_MASK (3 << 16)
  49. #define SLINK_CS_POLARITY1 (1 << 20)
  50. #define SLINK_CK_SDA (1 << 21)
  51. #define SLINK_CS_POLARITY2 (1 << 22)
  52. #define SLINK_CS_POLARITY3 (1 << 23)
  53. #define SLINK_IDLE_SCLK_DRIVE_LOW (0 << 24)
  54. #define SLINK_IDLE_SCLK_DRIVE_HIGH (1 << 24)
  55. #define SLINK_IDLE_SCLK_PULL_LOW (2 << 24)
  56. #define SLINK_IDLE_SCLK_PULL_HIGH (3 << 24)
  57. #define SLINK_IDLE_SCLK_MASK (3 << 24)
  58. #define SLINK_M_S (1 << 28)
  59. #define SLINK_WAIT (1 << 29)
  60. #define SLINK_GO (1 << 30)
  61. #define SLINK_ENB (1 << 31)
  62. #define SLINK_MODES (SLINK_IDLE_SCLK_MASK | SLINK_CK_SDA)
  63. #define SLINK_COMMAND2 0x004
  64. #define SLINK_LSBFE (1 << 0)
  65. #define SLINK_SSOE (1 << 1)
  66. #define SLINK_SPIE (1 << 4)
  67. #define SLINK_BIDIROE (1 << 6)
  68. #define SLINK_MODFEN (1 << 7)
  69. #define SLINK_INT_SIZE(x) (((x) & 0x1f) << 8)
  70. #define SLINK_CS_ACTIVE_BETWEEN (1 << 17)
  71. #define SLINK_SS_EN_CS(x) (((x) & 0x3) << 18)
  72. #define SLINK_SS_SETUP(x) (((x) & 0x3) << 20)
  73. #define SLINK_FIFO_REFILLS_0 (0 << 22)
  74. #define SLINK_FIFO_REFILLS_1 (1 << 22)
  75. #define SLINK_FIFO_REFILLS_2 (2 << 22)
  76. #define SLINK_FIFO_REFILLS_3 (3 << 22)
  77. #define SLINK_FIFO_REFILLS_MASK (3 << 22)
  78. #define SLINK_WAIT_PACK_INT(x) (((x) & 0x7) << 26)
  79. #define SLINK_SPC0 (1 << 29)
  80. #define SLINK_TXEN (1 << 30)
  81. #define SLINK_RXEN (1 << 31)
  82. #define SLINK_STATUS 0x008
  83. #define SLINK_COUNT(val) (((val) >> 0) & 0x1f)
  84. #define SLINK_WORD(val) (((val) >> 5) & 0x1f)
  85. #define SLINK_BLK_CNT(val) (((val) >> 0) & 0xffff)
  86. #define SLINK_MODF (1 << 16)
  87. #define SLINK_RX_UNF (1 << 18)
  88. #define SLINK_TX_OVF (1 << 19)
  89. #define SLINK_TX_FULL (1 << 20)
  90. #define SLINK_TX_EMPTY (1 << 21)
  91. #define SLINK_RX_FULL (1 << 22)
  92. #define SLINK_RX_EMPTY (1 << 23)
  93. #define SLINK_TX_UNF (1 << 24)
  94. #define SLINK_RX_OVF (1 << 25)
  95. #define SLINK_TX_FLUSH (1 << 26)
  96. #define SLINK_RX_FLUSH (1 << 27)
  97. #define SLINK_SCLK (1 << 28)
  98. #define SLINK_ERR (1 << 29)
  99. #define SLINK_RDY (1 << 30)
  100. #define SLINK_BSY (1 << 31)
  101. #define SLINK_FIFO_ERROR (SLINK_TX_OVF | SLINK_RX_UNF | \
  102. SLINK_TX_UNF | SLINK_RX_OVF)
  103. #define SLINK_FIFO_EMPTY (SLINK_TX_EMPTY | SLINK_RX_EMPTY)
  104. #define SLINK_MAS_DATA 0x010
  105. #define SLINK_SLAVE_DATA 0x014
  106. #define SLINK_DMA_CTL 0x018
  107. #define SLINK_DMA_BLOCK_SIZE(x) (((x) & 0xffff) << 0)
  108. #define SLINK_TX_TRIG_1 (0 << 16)
  109. #define SLINK_TX_TRIG_4 (1 << 16)
  110. #define SLINK_TX_TRIG_8 (2 << 16)
  111. #define SLINK_TX_TRIG_16 (3 << 16)
  112. #define SLINK_TX_TRIG_MASK (3 << 16)
  113. #define SLINK_RX_TRIG_1 (0 << 18)
  114. #define SLINK_RX_TRIG_4 (1 << 18)
  115. #define SLINK_RX_TRIG_8 (2 << 18)
  116. #define SLINK_RX_TRIG_16 (3 << 18)
  117. #define SLINK_RX_TRIG_MASK (3 << 18)
  118. #define SLINK_PACKED (1 << 20)
  119. #define SLINK_PACK_SIZE_4 (0 << 21)
  120. #define SLINK_PACK_SIZE_8 (1 << 21)
  121. #define SLINK_PACK_SIZE_16 (2 << 21)
  122. #define SLINK_PACK_SIZE_32 (3 << 21)
  123. #define SLINK_PACK_SIZE_MASK (3 << 21)
  124. #define SLINK_IE_TXC (1 << 26)
  125. #define SLINK_IE_RXC (1 << 27)
  126. #define SLINK_DMA_EN (1 << 31)
  127. #define SLINK_STATUS2 0x01c
  128. #define SLINK_TX_FIFO_EMPTY_COUNT(val) (((val) & 0x3f) >> 0)
  129. #define SLINK_RX_FIFO_FULL_COUNT(val) (((val) & 0x3f0000) >> 16)
  130. #define SLINK_SS_HOLD_TIME(val) (((val) & 0xF) << 6)
  131. #define SLINK_TX_FIFO 0x100
  132. #define SLINK_RX_FIFO 0x180
  133. #define DATA_DIR_TX (1 << 0)
  134. #define DATA_DIR_RX (1 << 1)
  135. #define SLINK_DMA_TIMEOUT (msecs_to_jiffies(1000))
  136. #define DEFAULT_SPI_DMA_BUF_LEN (16*1024)
  137. #define TX_FIFO_EMPTY_COUNT_MAX SLINK_TX_FIFO_EMPTY_COUNT(0x20)
  138. #define RX_FIFO_FULL_COUNT_ZERO SLINK_RX_FIFO_FULL_COUNT(0)
  139. #define SLINK_STATUS2_RESET \
  140. (TX_FIFO_EMPTY_COUNT_MAX | RX_FIFO_FULL_COUNT_ZERO << 16)
  141. #define MAX_CHIP_SELECT 4
  142. #define SLINK_FIFO_DEPTH 32
  143. struct tegra_slink_chip_data {
  144. bool cs_hold_time;
  145. };
  146. struct tegra_slink_data {
  147. struct device *dev;
  148. struct spi_master *master;
  149. const struct tegra_slink_chip_data *chip_data;
  150. spinlock_t lock;
  151. struct clk *clk;
  152. struct reset_control *rst;
  153. void __iomem *base;
  154. phys_addr_t phys;
  155. unsigned irq;
  156. u32 spi_max_frequency;
  157. u32 cur_speed;
  158. struct spi_device *cur_spi;
  159. unsigned cur_pos;
  160. unsigned cur_len;
  161. unsigned words_per_32bit;
  162. unsigned bytes_per_word;
  163. unsigned curr_dma_words;
  164. unsigned cur_direction;
  165. unsigned cur_rx_pos;
  166. unsigned cur_tx_pos;
  167. unsigned dma_buf_size;
  168. unsigned max_buf_size;
  169. bool is_curr_dma_xfer;
  170. struct completion rx_dma_complete;
  171. struct completion tx_dma_complete;
  172. u32 tx_status;
  173. u32 rx_status;
  174. u32 status_reg;
  175. bool is_packed;
  176. u32 packed_size;
  177. u32 command_reg;
  178. u32 command2_reg;
  179. u32 dma_control_reg;
  180. u32 def_command_reg;
  181. u32 def_command2_reg;
  182. struct completion xfer_completion;
  183. struct spi_transfer *curr_xfer;
  184. struct dma_chan *rx_dma_chan;
  185. u32 *rx_dma_buf;
  186. dma_addr_t rx_dma_phys;
  187. struct dma_async_tx_descriptor *rx_dma_desc;
  188. struct dma_chan *tx_dma_chan;
  189. u32 *tx_dma_buf;
  190. dma_addr_t tx_dma_phys;
  191. struct dma_async_tx_descriptor *tx_dma_desc;
  192. };
  193. static int tegra_slink_runtime_suspend(struct device *dev);
  194. static int tegra_slink_runtime_resume(struct device *dev);
  195. static inline u32 tegra_slink_readl(struct tegra_slink_data *tspi,
  196. unsigned long reg)
  197. {
  198. return readl(tspi->base + reg);
  199. }
  200. static inline void tegra_slink_writel(struct tegra_slink_data *tspi,
  201. u32 val, unsigned long reg)
  202. {
  203. writel(val, tspi->base + reg);
  204. /* Read back register to make sure that register writes completed */
  205. if (reg != SLINK_TX_FIFO)
  206. readl(tspi->base + SLINK_MAS_DATA);
  207. }
  208. static void tegra_slink_clear_status(struct tegra_slink_data *tspi)
  209. {
  210. u32 val_write;
  211. tegra_slink_readl(tspi, SLINK_STATUS);
  212. /* Write 1 to clear status register */
  213. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  214. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  215. }
  216. static u32 tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  217. struct spi_transfer *t)
  218. {
  219. switch (tspi->bytes_per_word) {
  220. case 0:
  221. return SLINK_PACK_SIZE_4;
  222. case 1:
  223. return SLINK_PACK_SIZE_8;
  224. case 2:
  225. return SLINK_PACK_SIZE_16;
  226. case 4:
  227. return SLINK_PACK_SIZE_32;
  228. default:
  229. return 0;
  230. }
  231. }
  232. static unsigned tegra_slink_calculate_curr_xfer_param(
  233. struct spi_device *spi, struct tegra_slink_data *tspi,
  234. struct spi_transfer *t)
  235. {
  236. unsigned remain_len = t->len - tspi->cur_pos;
  237. unsigned max_word;
  238. unsigned bits_per_word;
  239. unsigned max_len;
  240. unsigned total_fifo_words;
  241. bits_per_word = t->bits_per_word;
  242. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  243. if (bits_per_word == 8 || bits_per_word == 16) {
  244. tspi->is_packed = 1;
  245. tspi->words_per_32bit = 32/bits_per_word;
  246. } else {
  247. tspi->is_packed = 0;
  248. tspi->words_per_32bit = 1;
  249. }
  250. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  251. if (tspi->is_packed) {
  252. max_len = min(remain_len, tspi->max_buf_size);
  253. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  254. total_fifo_words = max_len/4;
  255. } else {
  256. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  257. max_word = min(max_word, tspi->max_buf_size/4);
  258. tspi->curr_dma_words = max_word;
  259. total_fifo_words = max_word;
  260. }
  261. return total_fifo_words;
  262. }
  263. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  264. struct tegra_slink_data *tspi, struct spi_transfer *t)
  265. {
  266. unsigned nbytes;
  267. unsigned tx_empty_count;
  268. u32 fifo_status;
  269. unsigned max_n_32bit;
  270. unsigned i, count;
  271. unsigned int written_words;
  272. unsigned fifo_words_left;
  273. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  274. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  275. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  276. if (tspi->is_packed) {
  277. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  278. written_words = min(fifo_words_left, tspi->curr_dma_words);
  279. nbytes = written_words * tspi->bytes_per_word;
  280. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  281. for (count = 0; count < max_n_32bit; count++) {
  282. u32 x = 0;
  283. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  284. x |= (u32)(*tx_buf++) << (i * 8);
  285. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  286. }
  287. } else {
  288. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  289. written_words = max_n_32bit;
  290. nbytes = written_words * tspi->bytes_per_word;
  291. for (count = 0; count < max_n_32bit; count++) {
  292. u32 x = 0;
  293. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  294. i++, nbytes--)
  295. x |= (u32)(*tx_buf++) << (i * 8);
  296. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  297. }
  298. }
  299. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  300. return written_words;
  301. }
  302. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  303. struct tegra_slink_data *tspi, struct spi_transfer *t)
  304. {
  305. unsigned rx_full_count;
  306. u32 fifo_status;
  307. unsigned i, count;
  308. unsigned int read_words = 0;
  309. unsigned len;
  310. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  311. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  312. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  313. if (tspi->is_packed) {
  314. len = tspi->curr_dma_words * tspi->bytes_per_word;
  315. for (count = 0; count < rx_full_count; count++) {
  316. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  317. for (i = 0; len && (i < 4); i++, len--)
  318. *rx_buf++ = (x >> i*8) & 0xFF;
  319. }
  320. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  321. read_words += tspi->curr_dma_words;
  322. } else {
  323. for (count = 0; count < rx_full_count; count++) {
  324. u32 x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  325. for (i = 0; (i < tspi->bytes_per_word); i++)
  326. *rx_buf++ = (x >> (i*8)) & 0xFF;
  327. }
  328. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  329. read_words += rx_full_count;
  330. }
  331. return read_words;
  332. }
  333. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  334. struct tegra_slink_data *tspi, struct spi_transfer *t)
  335. {
  336. /* Make the dma buffer to read by cpu */
  337. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  338. tspi->dma_buf_size, DMA_TO_DEVICE);
  339. if (tspi->is_packed) {
  340. unsigned len = tspi->curr_dma_words * tspi->bytes_per_word;
  341. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  342. } else {
  343. unsigned int i;
  344. unsigned int count;
  345. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  346. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  347. for (count = 0; count < tspi->curr_dma_words; count++) {
  348. u32 x = 0;
  349. for (i = 0; consume && (i < tspi->bytes_per_word);
  350. i++, consume--)
  351. x |= (u32)(*tx_buf++) << (i * 8);
  352. tspi->tx_dma_buf[count] = x;
  353. }
  354. }
  355. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  356. /* Make the dma buffer to read by dma */
  357. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  358. tspi->dma_buf_size, DMA_TO_DEVICE);
  359. }
  360. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  361. struct tegra_slink_data *tspi, struct spi_transfer *t)
  362. {
  363. unsigned len;
  364. /* Make the dma buffer to read by cpu */
  365. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  366. tspi->dma_buf_size, DMA_FROM_DEVICE);
  367. if (tspi->is_packed) {
  368. len = tspi->curr_dma_words * tspi->bytes_per_word;
  369. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  370. } else {
  371. unsigned int i;
  372. unsigned int count;
  373. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  374. u32 rx_mask = ((u32)1 << t->bits_per_word) - 1;
  375. for (count = 0; count < tspi->curr_dma_words; count++) {
  376. u32 x = tspi->rx_dma_buf[count] & rx_mask;
  377. for (i = 0; (i < tspi->bytes_per_word); i++)
  378. *rx_buf++ = (x >> (i*8)) & 0xFF;
  379. }
  380. }
  381. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  382. /* Make the dma buffer to read by dma */
  383. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  384. tspi->dma_buf_size, DMA_FROM_DEVICE);
  385. }
  386. static void tegra_slink_dma_complete(void *args)
  387. {
  388. struct completion *dma_complete = args;
  389. complete(dma_complete);
  390. }
  391. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  392. {
  393. reinit_completion(&tspi->tx_dma_complete);
  394. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  395. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  396. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  397. if (!tspi->tx_dma_desc) {
  398. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  399. return -EIO;
  400. }
  401. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  402. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  403. dmaengine_submit(tspi->tx_dma_desc);
  404. dma_async_issue_pending(tspi->tx_dma_chan);
  405. return 0;
  406. }
  407. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  408. {
  409. reinit_completion(&tspi->rx_dma_complete);
  410. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  411. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  412. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  413. if (!tspi->rx_dma_desc) {
  414. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  415. return -EIO;
  416. }
  417. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  418. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  419. dmaengine_submit(tspi->rx_dma_desc);
  420. dma_async_issue_pending(tspi->rx_dma_chan);
  421. return 0;
  422. }
  423. static int tegra_slink_start_dma_based_transfer(
  424. struct tegra_slink_data *tspi, struct spi_transfer *t)
  425. {
  426. u32 val;
  427. unsigned int len;
  428. int ret = 0;
  429. u32 status;
  430. /* Make sure that Rx and Tx fifo are empty */
  431. status = tegra_slink_readl(tspi, SLINK_STATUS);
  432. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  433. dev_err(tspi->dev, "Rx/Tx fifo are not empty status 0x%08x\n",
  434. (unsigned)status);
  435. return -EIO;
  436. }
  437. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  438. val |= tspi->packed_size;
  439. if (tspi->is_packed)
  440. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  441. 4) * 4;
  442. else
  443. len = tspi->curr_dma_words * 4;
  444. /* Set attention level based on length of transfer */
  445. if (len & 0xF)
  446. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  447. else if (((len) >> 4) & 0x1)
  448. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  449. else
  450. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  451. if (tspi->cur_direction & DATA_DIR_TX)
  452. val |= SLINK_IE_TXC;
  453. if (tspi->cur_direction & DATA_DIR_RX)
  454. val |= SLINK_IE_RXC;
  455. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  456. tspi->dma_control_reg = val;
  457. if (tspi->cur_direction & DATA_DIR_TX) {
  458. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  459. wmb();
  460. ret = tegra_slink_start_tx_dma(tspi, len);
  461. if (ret < 0) {
  462. dev_err(tspi->dev,
  463. "Starting tx dma failed, err %d\n", ret);
  464. return ret;
  465. }
  466. /* Wait for tx fifo to be fill before starting slink */
  467. status = tegra_slink_readl(tspi, SLINK_STATUS);
  468. while (!(status & SLINK_TX_FULL))
  469. status = tegra_slink_readl(tspi, SLINK_STATUS);
  470. }
  471. if (tspi->cur_direction & DATA_DIR_RX) {
  472. /* Make the dma buffer to read by dma */
  473. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  474. tspi->dma_buf_size, DMA_FROM_DEVICE);
  475. ret = tegra_slink_start_rx_dma(tspi, len);
  476. if (ret < 0) {
  477. dev_err(tspi->dev,
  478. "Starting rx dma failed, err %d\n", ret);
  479. if (tspi->cur_direction & DATA_DIR_TX)
  480. dmaengine_terminate_all(tspi->tx_dma_chan);
  481. return ret;
  482. }
  483. }
  484. tspi->is_curr_dma_xfer = true;
  485. if (tspi->is_packed) {
  486. val |= SLINK_PACKED;
  487. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  488. /* HW need small delay after settign Packed mode */
  489. udelay(1);
  490. }
  491. tspi->dma_control_reg = val;
  492. val |= SLINK_DMA_EN;
  493. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  494. return ret;
  495. }
  496. static int tegra_slink_start_cpu_based_transfer(
  497. struct tegra_slink_data *tspi, struct spi_transfer *t)
  498. {
  499. u32 val;
  500. unsigned cur_words;
  501. val = tspi->packed_size;
  502. if (tspi->cur_direction & DATA_DIR_TX)
  503. val |= SLINK_IE_TXC;
  504. if (tspi->cur_direction & DATA_DIR_RX)
  505. val |= SLINK_IE_RXC;
  506. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  507. tspi->dma_control_reg = val;
  508. if (tspi->cur_direction & DATA_DIR_TX)
  509. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  510. else
  511. cur_words = tspi->curr_dma_words;
  512. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  513. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  514. tspi->dma_control_reg = val;
  515. tspi->is_curr_dma_xfer = false;
  516. if (tspi->is_packed) {
  517. val |= SLINK_PACKED;
  518. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  519. udelay(1);
  520. wmb();
  521. }
  522. tspi->dma_control_reg = val;
  523. val |= SLINK_DMA_EN;
  524. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  525. return 0;
  526. }
  527. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  528. bool dma_to_memory)
  529. {
  530. struct dma_chan *dma_chan;
  531. u32 *dma_buf;
  532. dma_addr_t dma_phys;
  533. int ret;
  534. struct dma_slave_config dma_sconfig;
  535. dma_chan = dma_request_slave_channel_reason(tspi->dev,
  536. dma_to_memory ? "rx" : "tx");
  537. if (IS_ERR(dma_chan)) {
  538. ret = PTR_ERR(dma_chan);
  539. if (ret != -EPROBE_DEFER)
  540. dev_err(tspi->dev,
  541. "Dma channel is not available: %d\n", ret);
  542. return ret;
  543. }
  544. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  545. &dma_phys, GFP_KERNEL);
  546. if (!dma_buf) {
  547. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  548. dma_release_channel(dma_chan);
  549. return -ENOMEM;
  550. }
  551. if (dma_to_memory) {
  552. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  553. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  554. dma_sconfig.src_maxburst = 0;
  555. } else {
  556. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  557. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  558. dma_sconfig.dst_maxburst = 0;
  559. }
  560. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  561. if (ret)
  562. goto scrub;
  563. if (dma_to_memory) {
  564. tspi->rx_dma_chan = dma_chan;
  565. tspi->rx_dma_buf = dma_buf;
  566. tspi->rx_dma_phys = dma_phys;
  567. } else {
  568. tspi->tx_dma_chan = dma_chan;
  569. tspi->tx_dma_buf = dma_buf;
  570. tspi->tx_dma_phys = dma_phys;
  571. }
  572. return 0;
  573. scrub:
  574. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  575. dma_release_channel(dma_chan);
  576. return ret;
  577. }
  578. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  579. bool dma_to_memory)
  580. {
  581. u32 *dma_buf;
  582. dma_addr_t dma_phys;
  583. struct dma_chan *dma_chan;
  584. if (dma_to_memory) {
  585. dma_buf = tspi->rx_dma_buf;
  586. dma_chan = tspi->rx_dma_chan;
  587. dma_phys = tspi->rx_dma_phys;
  588. tspi->rx_dma_chan = NULL;
  589. tspi->rx_dma_buf = NULL;
  590. } else {
  591. dma_buf = tspi->tx_dma_buf;
  592. dma_chan = tspi->tx_dma_chan;
  593. dma_phys = tspi->tx_dma_phys;
  594. tspi->tx_dma_buf = NULL;
  595. tspi->tx_dma_chan = NULL;
  596. }
  597. if (!dma_chan)
  598. return;
  599. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  600. dma_release_channel(dma_chan);
  601. }
  602. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  603. struct spi_transfer *t)
  604. {
  605. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  606. u32 speed;
  607. u8 bits_per_word;
  608. unsigned total_fifo_words;
  609. int ret;
  610. u32 command;
  611. u32 command2;
  612. bits_per_word = t->bits_per_word;
  613. speed = t->speed_hz;
  614. if (speed != tspi->cur_speed) {
  615. clk_set_rate(tspi->clk, speed * 4);
  616. tspi->cur_speed = speed;
  617. }
  618. tspi->cur_spi = spi;
  619. tspi->cur_pos = 0;
  620. tspi->cur_rx_pos = 0;
  621. tspi->cur_tx_pos = 0;
  622. tspi->curr_xfer = t;
  623. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  624. command = tspi->command_reg;
  625. command &= ~SLINK_BIT_LENGTH(~0);
  626. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  627. command2 = tspi->command2_reg;
  628. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  629. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  630. tspi->command_reg = command;
  631. tspi->cur_direction = 0;
  632. if (t->rx_buf) {
  633. command2 |= SLINK_RXEN;
  634. tspi->cur_direction |= DATA_DIR_RX;
  635. }
  636. if (t->tx_buf) {
  637. command2 |= SLINK_TXEN;
  638. tspi->cur_direction |= DATA_DIR_TX;
  639. }
  640. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  641. tspi->command2_reg = command2;
  642. if (total_fifo_words > SLINK_FIFO_DEPTH)
  643. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  644. else
  645. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  646. return ret;
  647. }
  648. static int tegra_slink_setup(struct spi_device *spi)
  649. {
  650. static const u32 cs_pol_bit[MAX_CHIP_SELECT] = {
  651. SLINK_CS_POLARITY,
  652. SLINK_CS_POLARITY1,
  653. SLINK_CS_POLARITY2,
  654. SLINK_CS_POLARITY3,
  655. };
  656. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  657. u32 val;
  658. unsigned long flags;
  659. int ret;
  660. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  661. spi->bits_per_word,
  662. spi->mode & SPI_CPOL ? "" : "~",
  663. spi->mode & SPI_CPHA ? "" : "~",
  664. spi->max_speed_hz);
  665. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  666. /* Set speed to the spi max fequency if spi device has not set */
  667. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  668. ret = pm_runtime_get_sync(tspi->dev);
  669. if (ret < 0) {
  670. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  671. return ret;
  672. }
  673. spin_lock_irqsave(&tspi->lock, flags);
  674. val = tspi->def_command_reg;
  675. if (spi->mode & SPI_CS_HIGH)
  676. val |= cs_pol_bit[spi->chip_select];
  677. else
  678. val &= ~cs_pol_bit[spi->chip_select];
  679. tspi->def_command_reg = val;
  680. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  681. spin_unlock_irqrestore(&tspi->lock, flags);
  682. pm_runtime_put(tspi->dev);
  683. return 0;
  684. }
  685. static int tegra_slink_prepare_message(struct spi_master *master,
  686. struct spi_message *msg)
  687. {
  688. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  689. struct spi_device *spi = msg->spi;
  690. tegra_slink_clear_status(tspi);
  691. tspi->command_reg = tspi->def_command_reg;
  692. tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
  693. tspi->command2_reg = tspi->def_command2_reg;
  694. tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  695. tspi->command_reg &= ~SLINK_MODES;
  696. if (spi->mode & SPI_CPHA)
  697. tspi->command_reg |= SLINK_CK_SDA;
  698. if (spi->mode & SPI_CPOL)
  699. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  700. else
  701. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
  702. return 0;
  703. }
  704. static int tegra_slink_transfer_one(struct spi_master *master,
  705. struct spi_device *spi,
  706. struct spi_transfer *xfer)
  707. {
  708. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  709. int ret;
  710. reinit_completion(&tspi->xfer_completion);
  711. ret = tegra_slink_start_transfer_one(spi, xfer);
  712. if (ret < 0) {
  713. dev_err(tspi->dev,
  714. "spi can not start transfer, err %d\n", ret);
  715. return ret;
  716. }
  717. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  718. SLINK_DMA_TIMEOUT);
  719. if (WARN_ON(ret == 0)) {
  720. dev_err(tspi->dev,
  721. "spi trasfer timeout, err %d\n", ret);
  722. return -EIO;
  723. }
  724. if (tspi->tx_status)
  725. return tspi->tx_status;
  726. if (tspi->rx_status)
  727. return tspi->rx_status;
  728. return 0;
  729. }
  730. static int tegra_slink_unprepare_message(struct spi_master *master,
  731. struct spi_message *msg)
  732. {
  733. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  734. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  735. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  736. return 0;
  737. }
  738. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  739. {
  740. struct spi_transfer *t = tspi->curr_xfer;
  741. unsigned long flags;
  742. spin_lock_irqsave(&tspi->lock, flags);
  743. if (tspi->tx_status || tspi->rx_status ||
  744. (tspi->status_reg & SLINK_BSY)) {
  745. dev_err(tspi->dev,
  746. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  747. dev_err(tspi->dev,
  748. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  749. tspi->command2_reg, tspi->dma_control_reg);
  750. reset_control_assert(tspi->rst);
  751. udelay(2);
  752. reset_control_deassert(tspi->rst);
  753. complete(&tspi->xfer_completion);
  754. goto exit;
  755. }
  756. if (tspi->cur_direction & DATA_DIR_RX)
  757. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  758. if (tspi->cur_direction & DATA_DIR_TX)
  759. tspi->cur_pos = tspi->cur_tx_pos;
  760. else
  761. tspi->cur_pos = tspi->cur_rx_pos;
  762. if (tspi->cur_pos == t->len) {
  763. complete(&tspi->xfer_completion);
  764. goto exit;
  765. }
  766. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  767. tegra_slink_start_cpu_based_transfer(tspi, t);
  768. exit:
  769. spin_unlock_irqrestore(&tspi->lock, flags);
  770. return IRQ_HANDLED;
  771. }
  772. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  773. {
  774. struct spi_transfer *t = tspi->curr_xfer;
  775. long wait_status;
  776. int err = 0;
  777. unsigned total_fifo_words;
  778. unsigned long flags;
  779. /* Abort dmas if any error */
  780. if (tspi->cur_direction & DATA_DIR_TX) {
  781. if (tspi->tx_status) {
  782. dmaengine_terminate_all(tspi->tx_dma_chan);
  783. err += 1;
  784. } else {
  785. wait_status = wait_for_completion_interruptible_timeout(
  786. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  787. if (wait_status <= 0) {
  788. dmaengine_terminate_all(tspi->tx_dma_chan);
  789. dev_err(tspi->dev, "TxDma Xfer failed\n");
  790. err += 1;
  791. }
  792. }
  793. }
  794. if (tspi->cur_direction & DATA_DIR_RX) {
  795. if (tspi->rx_status) {
  796. dmaengine_terminate_all(tspi->rx_dma_chan);
  797. err += 2;
  798. } else {
  799. wait_status = wait_for_completion_interruptible_timeout(
  800. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  801. if (wait_status <= 0) {
  802. dmaengine_terminate_all(tspi->rx_dma_chan);
  803. dev_err(tspi->dev, "RxDma Xfer failed\n");
  804. err += 2;
  805. }
  806. }
  807. }
  808. spin_lock_irqsave(&tspi->lock, flags);
  809. if (err) {
  810. dev_err(tspi->dev,
  811. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  812. dev_err(tspi->dev,
  813. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  814. tspi->command2_reg, tspi->dma_control_reg);
  815. reset_control_assert(tspi->rst);
  816. udelay(2);
  817. reset_control_assert(tspi->rst);
  818. complete(&tspi->xfer_completion);
  819. spin_unlock_irqrestore(&tspi->lock, flags);
  820. return IRQ_HANDLED;
  821. }
  822. if (tspi->cur_direction & DATA_DIR_RX)
  823. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  824. if (tspi->cur_direction & DATA_DIR_TX)
  825. tspi->cur_pos = tspi->cur_tx_pos;
  826. else
  827. tspi->cur_pos = tspi->cur_rx_pos;
  828. if (tspi->cur_pos == t->len) {
  829. complete(&tspi->xfer_completion);
  830. goto exit;
  831. }
  832. /* Continue transfer in current message */
  833. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  834. tspi, t);
  835. if (total_fifo_words > SLINK_FIFO_DEPTH)
  836. err = tegra_slink_start_dma_based_transfer(tspi, t);
  837. else
  838. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  839. exit:
  840. spin_unlock_irqrestore(&tspi->lock, flags);
  841. return IRQ_HANDLED;
  842. }
  843. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  844. {
  845. struct tegra_slink_data *tspi = context_data;
  846. if (!tspi->is_curr_dma_xfer)
  847. return handle_cpu_based_xfer(tspi);
  848. return handle_dma_based_xfer(tspi);
  849. }
  850. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  851. {
  852. struct tegra_slink_data *tspi = context_data;
  853. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  854. if (tspi->cur_direction & DATA_DIR_TX)
  855. tspi->tx_status = tspi->status_reg &
  856. (SLINK_TX_OVF | SLINK_TX_UNF);
  857. if (tspi->cur_direction & DATA_DIR_RX)
  858. tspi->rx_status = tspi->status_reg &
  859. (SLINK_RX_OVF | SLINK_RX_UNF);
  860. tegra_slink_clear_status(tspi);
  861. return IRQ_WAKE_THREAD;
  862. }
  863. static void tegra_slink_parse_dt(struct tegra_slink_data *tspi)
  864. {
  865. struct device_node *np = tspi->dev->of_node;
  866. if (of_property_read_u32(np, "spi-max-frequency",
  867. &tspi->spi_max_frequency))
  868. tspi->spi_max_frequency = 25000000; /* 25MHz */
  869. }
  870. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  871. .cs_hold_time = true,
  872. };
  873. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  874. .cs_hold_time = false,
  875. };
  876. static struct of_device_id tegra_slink_of_match[] = {
  877. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  878. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  879. {}
  880. };
  881. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  882. static int tegra_slink_probe(struct platform_device *pdev)
  883. {
  884. struct spi_master *master;
  885. struct tegra_slink_data *tspi;
  886. struct resource *r;
  887. int ret, spi_irq;
  888. const struct tegra_slink_chip_data *cdata = NULL;
  889. const struct of_device_id *match;
  890. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  891. if (!match) {
  892. dev_err(&pdev->dev, "Error: No device match found\n");
  893. return -ENODEV;
  894. }
  895. cdata = match->data;
  896. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  897. if (!master) {
  898. dev_err(&pdev->dev, "master allocation failed\n");
  899. return -ENOMEM;
  900. }
  901. /* the spi->mode bits understood by this driver: */
  902. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  903. master->setup = tegra_slink_setup;
  904. master->prepare_message = tegra_slink_prepare_message;
  905. master->transfer_one = tegra_slink_transfer_one;
  906. master->unprepare_message = tegra_slink_unprepare_message;
  907. master->auto_runtime_pm = true;
  908. master->num_chipselect = MAX_CHIP_SELECT;
  909. platform_set_drvdata(pdev, master);
  910. tspi = spi_master_get_devdata(master);
  911. tspi->master = master;
  912. tspi->dev = &pdev->dev;
  913. tspi->chip_data = cdata;
  914. spin_lock_init(&tspi->lock);
  915. tegra_slink_parse_dt(tspi);
  916. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  917. if (!r) {
  918. dev_err(&pdev->dev, "No IO memory resource\n");
  919. ret = -ENODEV;
  920. goto exit_free_master;
  921. }
  922. tspi->phys = r->start;
  923. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  924. if (IS_ERR(tspi->base)) {
  925. ret = PTR_ERR(tspi->base);
  926. goto exit_free_master;
  927. }
  928. spi_irq = platform_get_irq(pdev, 0);
  929. tspi->irq = spi_irq;
  930. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  931. tegra_slink_isr_thread, IRQF_ONESHOT,
  932. dev_name(&pdev->dev), tspi);
  933. if (ret < 0) {
  934. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  935. tspi->irq);
  936. goto exit_free_master;
  937. }
  938. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  939. if (IS_ERR(tspi->clk)) {
  940. dev_err(&pdev->dev, "can not get clock\n");
  941. ret = PTR_ERR(tspi->clk);
  942. goto exit_free_irq;
  943. }
  944. tspi->rst = devm_reset_control_get(&pdev->dev, "spi");
  945. if (IS_ERR(tspi->rst)) {
  946. dev_err(&pdev->dev, "can not get reset\n");
  947. ret = PTR_ERR(tspi->rst);
  948. goto exit_free_irq;
  949. }
  950. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  951. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  952. ret = tegra_slink_init_dma_param(tspi, true);
  953. if (ret < 0)
  954. goto exit_free_irq;
  955. ret = tegra_slink_init_dma_param(tspi, false);
  956. if (ret < 0)
  957. goto exit_rx_dma_free;
  958. tspi->max_buf_size = tspi->dma_buf_size;
  959. init_completion(&tspi->tx_dma_complete);
  960. init_completion(&tspi->rx_dma_complete);
  961. init_completion(&tspi->xfer_completion);
  962. pm_runtime_enable(&pdev->dev);
  963. if (!pm_runtime_enabled(&pdev->dev)) {
  964. ret = tegra_slink_runtime_resume(&pdev->dev);
  965. if (ret)
  966. goto exit_pm_disable;
  967. }
  968. ret = pm_runtime_get_sync(&pdev->dev);
  969. if (ret < 0) {
  970. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  971. goto exit_pm_disable;
  972. }
  973. tspi->def_command_reg = SLINK_M_S;
  974. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  975. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  976. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  977. pm_runtime_put(&pdev->dev);
  978. master->dev.of_node = pdev->dev.of_node;
  979. ret = devm_spi_register_master(&pdev->dev, master);
  980. if (ret < 0) {
  981. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  982. goto exit_pm_disable;
  983. }
  984. return ret;
  985. exit_pm_disable:
  986. pm_runtime_disable(&pdev->dev);
  987. if (!pm_runtime_status_suspended(&pdev->dev))
  988. tegra_slink_runtime_suspend(&pdev->dev);
  989. tegra_slink_deinit_dma_param(tspi, false);
  990. exit_rx_dma_free:
  991. tegra_slink_deinit_dma_param(tspi, true);
  992. exit_free_irq:
  993. free_irq(spi_irq, tspi);
  994. exit_free_master:
  995. spi_master_put(master);
  996. return ret;
  997. }
  998. static int tegra_slink_remove(struct platform_device *pdev)
  999. {
  1000. struct spi_master *master = platform_get_drvdata(pdev);
  1001. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1002. free_irq(tspi->irq, tspi);
  1003. if (tspi->tx_dma_chan)
  1004. tegra_slink_deinit_dma_param(tspi, false);
  1005. if (tspi->rx_dma_chan)
  1006. tegra_slink_deinit_dma_param(tspi, true);
  1007. pm_runtime_disable(&pdev->dev);
  1008. if (!pm_runtime_status_suspended(&pdev->dev))
  1009. tegra_slink_runtime_suspend(&pdev->dev);
  1010. return 0;
  1011. }
  1012. #ifdef CONFIG_PM_SLEEP
  1013. static int tegra_slink_suspend(struct device *dev)
  1014. {
  1015. struct spi_master *master = dev_get_drvdata(dev);
  1016. return spi_master_suspend(master);
  1017. }
  1018. static int tegra_slink_resume(struct device *dev)
  1019. {
  1020. struct spi_master *master = dev_get_drvdata(dev);
  1021. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1022. int ret;
  1023. ret = pm_runtime_get_sync(dev);
  1024. if (ret < 0) {
  1025. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1026. return ret;
  1027. }
  1028. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1029. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1030. pm_runtime_put(dev);
  1031. return spi_master_resume(master);
  1032. }
  1033. #endif
  1034. static int tegra_slink_runtime_suspend(struct device *dev)
  1035. {
  1036. struct spi_master *master = dev_get_drvdata(dev);
  1037. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1038. /* Flush all write which are in PPSB queue by reading back */
  1039. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1040. clk_disable_unprepare(tspi->clk);
  1041. return 0;
  1042. }
  1043. static int tegra_slink_runtime_resume(struct device *dev)
  1044. {
  1045. struct spi_master *master = dev_get_drvdata(dev);
  1046. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1047. int ret;
  1048. ret = clk_prepare_enable(tspi->clk);
  1049. if (ret < 0) {
  1050. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1051. return ret;
  1052. }
  1053. return 0;
  1054. }
  1055. static const struct dev_pm_ops slink_pm_ops = {
  1056. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1057. tegra_slink_runtime_resume, NULL)
  1058. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1059. };
  1060. static struct platform_driver tegra_slink_driver = {
  1061. .driver = {
  1062. .name = "spi-tegra-slink",
  1063. .owner = THIS_MODULE,
  1064. .pm = &slink_pm_ops,
  1065. .of_match_table = tegra_slink_of_match,
  1066. },
  1067. .probe = tegra_slink_probe,
  1068. .remove = tegra_slink_remove,
  1069. };
  1070. module_platform_driver(tegra_slink_driver);
  1071. MODULE_ALIAS("platform:spi-tegra-slink");
  1072. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1073. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1074. MODULE_LICENSE("GPL v2");