spi-tegra20-slink.c 34 KB

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