spi-tegra20-slink.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276
  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/spi/spi.h>
  36. #include <linux/clk/tegra.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. void __iomem *base;
  153. phys_addr_t phys;
  154. unsigned irq;
  155. int dma_req_sel;
  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. unsigned long 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 unsigned long 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. unsigned long 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. unsigned long val;
  211. unsigned long val_write = 0;
  212. val = tegra_slink_readl(tspi, SLINK_STATUS);
  213. /* Write 1 to clear status register */
  214. val_write = SLINK_RDY | SLINK_FIFO_ERROR;
  215. tegra_slink_writel(tspi, val_write, SLINK_STATUS);
  216. }
  217. static unsigned long tegra_slink_get_packed_size(struct tegra_slink_data *tspi,
  218. struct spi_transfer *t)
  219. {
  220. unsigned long val;
  221. switch (tspi->bytes_per_word) {
  222. case 0:
  223. val = SLINK_PACK_SIZE_4;
  224. break;
  225. case 1:
  226. val = SLINK_PACK_SIZE_8;
  227. break;
  228. case 2:
  229. val = SLINK_PACK_SIZE_16;
  230. break;
  231. case 4:
  232. val = SLINK_PACK_SIZE_32;
  233. break;
  234. default:
  235. val = 0;
  236. }
  237. return val;
  238. }
  239. static unsigned tegra_slink_calculate_curr_xfer_param(
  240. struct spi_device *spi, struct tegra_slink_data *tspi,
  241. struct spi_transfer *t)
  242. {
  243. unsigned remain_len = t->len - tspi->cur_pos;
  244. unsigned max_word;
  245. unsigned bits_per_word;
  246. unsigned max_len;
  247. unsigned total_fifo_words;
  248. bits_per_word = t->bits_per_word;
  249. tspi->bytes_per_word = DIV_ROUND_UP(bits_per_word, 8);
  250. if (bits_per_word == 8 || bits_per_word == 16) {
  251. tspi->is_packed = 1;
  252. tspi->words_per_32bit = 32/bits_per_word;
  253. } else {
  254. tspi->is_packed = 0;
  255. tspi->words_per_32bit = 1;
  256. }
  257. tspi->packed_size = tegra_slink_get_packed_size(tspi, t);
  258. if (tspi->is_packed) {
  259. max_len = min(remain_len, tspi->max_buf_size);
  260. tspi->curr_dma_words = max_len/tspi->bytes_per_word;
  261. total_fifo_words = max_len/4;
  262. } else {
  263. max_word = (remain_len - 1) / tspi->bytes_per_word + 1;
  264. max_word = min(max_word, tspi->max_buf_size/4);
  265. tspi->curr_dma_words = max_word;
  266. total_fifo_words = max_word;
  267. }
  268. return total_fifo_words;
  269. }
  270. static unsigned tegra_slink_fill_tx_fifo_from_client_txbuf(
  271. struct tegra_slink_data *tspi, struct spi_transfer *t)
  272. {
  273. unsigned nbytes;
  274. unsigned tx_empty_count;
  275. unsigned long fifo_status;
  276. unsigned max_n_32bit;
  277. unsigned i, count;
  278. unsigned long x;
  279. unsigned int written_words;
  280. unsigned fifo_words_left;
  281. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  282. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  283. tx_empty_count = SLINK_TX_FIFO_EMPTY_COUNT(fifo_status);
  284. if (tspi->is_packed) {
  285. fifo_words_left = tx_empty_count * tspi->words_per_32bit;
  286. written_words = min(fifo_words_left, tspi->curr_dma_words);
  287. nbytes = written_words * tspi->bytes_per_word;
  288. max_n_32bit = DIV_ROUND_UP(nbytes, 4);
  289. for (count = 0; count < max_n_32bit; count++) {
  290. x = 0;
  291. for (i = 0; (i < 4) && nbytes; i++, nbytes--)
  292. x |= (*tx_buf++) << (i*8);
  293. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  294. }
  295. } else {
  296. max_n_32bit = min(tspi->curr_dma_words, tx_empty_count);
  297. written_words = max_n_32bit;
  298. nbytes = written_words * tspi->bytes_per_word;
  299. for (count = 0; count < max_n_32bit; count++) {
  300. x = 0;
  301. for (i = 0; nbytes && (i < tspi->bytes_per_word);
  302. i++, nbytes--)
  303. x |= ((*tx_buf++) << i*8);
  304. tegra_slink_writel(tspi, x, SLINK_TX_FIFO);
  305. }
  306. }
  307. tspi->cur_tx_pos += written_words * tspi->bytes_per_word;
  308. return written_words;
  309. }
  310. static unsigned int tegra_slink_read_rx_fifo_to_client_rxbuf(
  311. struct tegra_slink_data *tspi, struct spi_transfer *t)
  312. {
  313. unsigned rx_full_count;
  314. unsigned long fifo_status;
  315. unsigned i, count;
  316. unsigned long x;
  317. unsigned int read_words = 0;
  318. unsigned len;
  319. u8 *rx_buf = (u8 *)t->rx_buf + tspi->cur_rx_pos;
  320. fifo_status = tegra_slink_readl(tspi, SLINK_STATUS2);
  321. rx_full_count = SLINK_RX_FIFO_FULL_COUNT(fifo_status);
  322. if (tspi->is_packed) {
  323. len = tspi->curr_dma_words * tspi->bytes_per_word;
  324. for (count = 0; count < rx_full_count; count++) {
  325. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  326. for (i = 0; len && (i < 4); i++, len--)
  327. *rx_buf++ = (x >> i*8) & 0xFF;
  328. }
  329. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  330. read_words += tspi->curr_dma_words;
  331. } else {
  332. for (count = 0; count < rx_full_count; count++) {
  333. x = tegra_slink_readl(tspi, SLINK_RX_FIFO);
  334. for (i = 0; (i < tspi->bytes_per_word); i++)
  335. *rx_buf++ = (x >> (i*8)) & 0xFF;
  336. }
  337. tspi->cur_rx_pos += rx_full_count * tspi->bytes_per_word;
  338. read_words += rx_full_count;
  339. }
  340. return read_words;
  341. }
  342. static void tegra_slink_copy_client_txbuf_to_spi_txbuf(
  343. struct tegra_slink_data *tspi, struct spi_transfer *t)
  344. {
  345. unsigned len;
  346. /* Make the dma buffer to read by cpu */
  347. dma_sync_single_for_cpu(tspi->dev, tspi->tx_dma_phys,
  348. tspi->dma_buf_size, DMA_TO_DEVICE);
  349. if (tspi->is_packed) {
  350. len = tspi->curr_dma_words * tspi->bytes_per_word;
  351. memcpy(tspi->tx_dma_buf, t->tx_buf + tspi->cur_pos, len);
  352. } else {
  353. unsigned int i;
  354. unsigned int count;
  355. u8 *tx_buf = (u8 *)t->tx_buf + tspi->cur_tx_pos;
  356. unsigned consume = tspi->curr_dma_words * tspi->bytes_per_word;
  357. unsigned int x;
  358. for (count = 0; count < tspi->curr_dma_words; count++) {
  359. x = 0;
  360. for (i = 0; consume && (i < tspi->bytes_per_word);
  361. i++, consume--)
  362. x |= ((*tx_buf++) << i * 8);
  363. tspi->tx_dma_buf[count] = x;
  364. }
  365. }
  366. tspi->cur_tx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  367. /* Make the dma buffer to read by dma */
  368. dma_sync_single_for_device(tspi->dev, tspi->tx_dma_phys,
  369. tspi->dma_buf_size, DMA_TO_DEVICE);
  370. }
  371. static void tegra_slink_copy_spi_rxbuf_to_client_rxbuf(
  372. struct tegra_slink_data *tspi, struct spi_transfer *t)
  373. {
  374. unsigned len;
  375. /* Make the dma buffer to read by cpu */
  376. dma_sync_single_for_cpu(tspi->dev, tspi->rx_dma_phys,
  377. tspi->dma_buf_size, DMA_FROM_DEVICE);
  378. if (tspi->is_packed) {
  379. len = tspi->curr_dma_words * tspi->bytes_per_word;
  380. memcpy(t->rx_buf + tspi->cur_rx_pos, tspi->rx_dma_buf, len);
  381. } else {
  382. unsigned int i;
  383. unsigned int count;
  384. unsigned char *rx_buf = t->rx_buf + tspi->cur_rx_pos;
  385. unsigned int x;
  386. unsigned int rx_mask, bits_per_word;
  387. bits_per_word = t->bits_per_word;
  388. rx_mask = (1 << bits_per_word) - 1;
  389. for (count = 0; count < tspi->curr_dma_words; count++) {
  390. x = tspi->rx_dma_buf[count];
  391. x &= rx_mask;
  392. for (i = 0; (i < tspi->bytes_per_word); i++)
  393. *rx_buf++ = (x >> (i*8)) & 0xFF;
  394. }
  395. }
  396. tspi->cur_rx_pos += tspi->curr_dma_words * tspi->bytes_per_word;
  397. /* Make the dma buffer to read by dma */
  398. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  399. tspi->dma_buf_size, DMA_FROM_DEVICE);
  400. }
  401. static void tegra_slink_dma_complete(void *args)
  402. {
  403. struct completion *dma_complete = args;
  404. complete(dma_complete);
  405. }
  406. static int tegra_slink_start_tx_dma(struct tegra_slink_data *tspi, int len)
  407. {
  408. INIT_COMPLETION(tspi->tx_dma_complete);
  409. tspi->tx_dma_desc = dmaengine_prep_slave_single(tspi->tx_dma_chan,
  410. tspi->tx_dma_phys, len, DMA_MEM_TO_DEV,
  411. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  412. if (!tspi->tx_dma_desc) {
  413. dev_err(tspi->dev, "Not able to get desc for Tx\n");
  414. return -EIO;
  415. }
  416. tspi->tx_dma_desc->callback = tegra_slink_dma_complete;
  417. tspi->tx_dma_desc->callback_param = &tspi->tx_dma_complete;
  418. dmaengine_submit(tspi->tx_dma_desc);
  419. dma_async_issue_pending(tspi->tx_dma_chan);
  420. return 0;
  421. }
  422. static int tegra_slink_start_rx_dma(struct tegra_slink_data *tspi, int len)
  423. {
  424. INIT_COMPLETION(tspi->rx_dma_complete);
  425. tspi->rx_dma_desc = dmaengine_prep_slave_single(tspi->rx_dma_chan,
  426. tspi->rx_dma_phys, len, DMA_DEV_TO_MEM,
  427. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  428. if (!tspi->rx_dma_desc) {
  429. dev_err(tspi->dev, "Not able to get desc for Rx\n");
  430. return -EIO;
  431. }
  432. tspi->rx_dma_desc->callback = tegra_slink_dma_complete;
  433. tspi->rx_dma_desc->callback_param = &tspi->rx_dma_complete;
  434. dmaengine_submit(tspi->rx_dma_desc);
  435. dma_async_issue_pending(tspi->rx_dma_chan);
  436. return 0;
  437. }
  438. static int tegra_slink_start_dma_based_transfer(
  439. struct tegra_slink_data *tspi, struct spi_transfer *t)
  440. {
  441. unsigned long val;
  442. unsigned long test_val;
  443. unsigned int len;
  444. int ret = 0;
  445. unsigned long status;
  446. /* Make sure that Rx and Tx fifo are empty */
  447. status = tegra_slink_readl(tspi, SLINK_STATUS);
  448. if ((status & SLINK_FIFO_EMPTY) != SLINK_FIFO_EMPTY) {
  449. dev_err(tspi->dev,
  450. "Rx/Tx fifo are not empty status 0x%08lx\n", status);
  451. return -EIO;
  452. }
  453. val = SLINK_DMA_BLOCK_SIZE(tspi->curr_dma_words - 1);
  454. val |= tspi->packed_size;
  455. if (tspi->is_packed)
  456. len = DIV_ROUND_UP(tspi->curr_dma_words * tspi->bytes_per_word,
  457. 4) * 4;
  458. else
  459. len = tspi->curr_dma_words * 4;
  460. /* Set attention level based on length of transfer */
  461. if (len & 0xF)
  462. val |= SLINK_TX_TRIG_1 | SLINK_RX_TRIG_1;
  463. else if (((len) >> 4) & 0x1)
  464. val |= SLINK_TX_TRIG_4 | SLINK_RX_TRIG_4;
  465. else
  466. val |= SLINK_TX_TRIG_8 | SLINK_RX_TRIG_8;
  467. if (tspi->cur_direction & DATA_DIR_TX)
  468. val |= SLINK_IE_TXC;
  469. if (tspi->cur_direction & DATA_DIR_RX)
  470. val |= SLINK_IE_RXC;
  471. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  472. tspi->dma_control_reg = val;
  473. if (tspi->cur_direction & DATA_DIR_TX) {
  474. tegra_slink_copy_client_txbuf_to_spi_txbuf(tspi, t);
  475. wmb();
  476. ret = tegra_slink_start_tx_dma(tspi, len);
  477. if (ret < 0) {
  478. dev_err(tspi->dev,
  479. "Starting tx dma failed, err %d\n", ret);
  480. return ret;
  481. }
  482. /* Wait for tx fifo to be fill before starting slink */
  483. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  484. while (!(test_val & SLINK_TX_FULL))
  485. test_val = tegra_slink_readl(tspi, SLINK_STATUS);
  486. }
  487. if (tspi->cur_direction & DATA_DIR_RX) {
  488. /* Make the dma buffer to read by dma */
  489. dma_sync_single_for_device(tspi->dev, tspi->rx_dma_phys,
  490. tspi->dma_buf_size, DMA_FROM_DEVICE);
  491. ret = tegra_slink_start_rx_dma(tspi, len);
  492. if (ret < 0) {
  493. dev_err(tspi->dev,
  494. "Starting rx dma failed, err %d\n", ret);
  495. if (tspi->cur_direction & DATA_DIR_TX)
  496. dmaengine_terminate_all(tspi->tx_dma_chan);
  497. return ret;
  498. }
  499. }
  500. tspi->is_curr_dma_xfer = true;
  501. if (tspi->is_packed) {
  502. val |= SLINK_PACKED;
  503. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  504. /* HW need small delay after settign Packed mode */
  505. udelay(1);
  506. }
  507. tspi->dma_control_reg = val;
  508. val |= SLINK_DMA_EN;
  509. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  510. return ret;
  511. }
  512. static int tegra_slink_start_cpu_based_transfer(
  513. struct tegra_slink_data *tspi, struct spi_transfer *t)
  514. {
  515. unsigned long val;
  516. unsigned cur_words;
  517. val = tspi->packed_size;
  518. if (tspi->cur_direction & DATA_DIR_TX)
  519. val |= SLINK_IE_TXC;
  520. if (tspi->cur_direction & DATA_DIR_RX)
  521. val |= SLINK_IE_RXC;
  522. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  523. tspi->dma_control_reg = val;
  524. if (tspi->cur_direction & DATA_DIR_TX)
  525. cur_words = tegra_slink_fill_tx_fifo_from_client_txbuf(tspi, t);
  526. else
  527. cur_words = tspi->curr_dma_words;
  528. val |= SLINK_DMA_BLOCK_SIZE(cur_words - 1);
  529. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  530. tspi->dma_control_reg = val;
  531. tspi->is_curr_dma_xfer = false;
  532. if (tspi->is_packed) {
  533. val |= SLINK_PACKED;
  534. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  535. udelay(1);
  536. wmb();
  537. }
  538. tspi->dma_control_reg = val;
  539. val |= SLINK_DMA_EN;
  540. tegra_slink_writel(tspi, val, SLINK_DMA_CTL);
  541. return 0;
  542. }
  543. static int tegra_slink_init_dma_param(struct tegra_slink_data *tspi,
  544. bool dma_to_memory)
  545. {
  546. struct dma_chan *dma_chan;
  547. u32 *dma_buf;
  548. dma_addr_t dma_phys;
  549. int ret;
  550. struct dma_slave_config dma_sconfig;
  551. dma_cap_mask_t mask;
  552. dma_cap_zero(mask);
  553. dma_cap_set(DMA_SLAVE, mask);
  554. dma_chan = dma_request_channel(mask, NULL, NULL);
  555. if (!dma_chan) {
  556. dev_err(tspi->dev,
  557. "Dma channel is not available, will try later\n");
  558. return -EPROBE_DEFER;
  559. }
  560. dma_buf = dma_alloc_coherent(tspi->dev, tspi->dma_buf_size,
  561. &dma_phys, GFP_KERNEL);
  562. if (!dma_buf) {
  563. dev_err(tspi->dev, " Not able to allocate the dma buffer\n");
  564. dma_release_channel(dma_chan);
  565. return -ENOMEM;
  566. }
  567. dma_sconfig.slave_id = tspi->dma_req_sel;
  568. if (dma_to_memory) {
  569. dma_sconfig.src_addr = tspi->phys + SLINK_RX_FIFO;
  570. dma_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  571. dma_sconfig.src_maxburst = 0;
  572. } else {
  573. dma_sconfig.dst_addr = tspi->phys + SLINK_TX_FIFO;
  574. dma_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  575. dma_sconfig.dst_maxburst = 0;
  576. }
  577. ret = dmaengine_slave_config(dma_chan, &dma_sconfig);
  578. if (ret)
  579. goto scrub;
  580. if (dma_to_memory) {
  581. tspi->rx_dma_chan = dma_chan;
  582. tspi->rx_dma_buf = dma_buf;
  583. tspi->rx_dma_phys = dma_phys;
  584. } else {
  585. tspi->tx_dma_chan = dma_chan;
  586. tspi->tx_dma_buf = dma_buf;
  587. tspi->tx_dma_phys = dma_phys;
  588. }
  589. return 0;
  590. scrub:
  591. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  592. dma_release_channel(dma_chan);
  593. return ret;
  594. }
  595. static void tegra_slink_deinit_dma_param(struct tegra_slink_data *tspi,
  596. bool dma_to_memory)
  597. {
  598. u32 *dma_buf;
  599. dma_addr_t dma_phys;
  600. struct dma_chan *dma_chan;
  601. if (dma_to_memory) {
  602. dma_buf = tspi->rx_dma_buf;
  603. dma_chan = tspi->rx_dma_chan;
  604. dma_phys = tspi->rx_dma_phys;
  605. tspi->rx_dma_chan = NULL;
  606. tspi->rx_dma_buf = NULL;
  607. } else {
  608. dma_buf = tspi->tx_dma_buf;
  609. dma_chan = tspi->tx_dma_chan;
  610. dma_phys = tspi->tx_dma_phys;
  611. tspi->tx_dma_buf = NULL;
  612. tspi->tx_dma_chan = NULL;
  613. }
  614. if (!dma_chan)
  615. return;
  616. dma_free_coherent(tspi->dev, tspi->dma_buf_size, dma_buf, dma_phys);
  617. dma_release_channel(dma_chan);
  618. }
  619. static int tegra_slink_start_transfer_one(struct spi_device *spi,
  620. struct spi_transfer *t)
  621. {
  622. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  623. u32 speed;
  624. u8 bits_per_word;
  625. unsigned total_fifo_words;
  626. int ret;
  627. unsigned long command;
  628. unsigned long command2;
  629. bits_per_word = t->bits_per_word;
  630. speed = t->speed_hz;
  631. if (speed != tspi->cur_speed) {
  632. clk_set_rate(tspi->clk, speed * 4);
  633. tspi->cur_speed = speed;
  634. }
  635. tspi->cur_spi = spi;
  636. tspi->cur_pos = 0;
  637. tspi->cur_rx_pos = 0;
  638. tspi->cur_tx_pos = 0;
  639. tspi->curr_xfer = t;
  640. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  641. command = tspi->command_reg;
  642. command &= ~SLINK_BIT_LENGTH(~0);
  643. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  644. command2 = tspi->command2_reg;
  645. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  646. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  647. tspi->command_reg = command;
  648. tspi->cur_direction = 0;
  649. if (t->rx_buf) {
  650. command2 |= SLINK_RXEN;
  651. tspi->cur_direction |= DATA_DIR_RX;
  652. }
  653. if (t->tx_buf) {
  654. command2 |= SLINK_TXEN;
  655. tspi->cur_direction |= DATA_DIR_TX;
  656. }
  657. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  658. tspi->command2_reg = command2;
  659. if (total_fifo_words > SLINK_FIFO_DEPTH)
  660. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  661. else
  662. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  663. return ret;
  664. }
  665. static int tegra_slink_setup(struct spi_device *spi)
  666. {
  667. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  668. unsigned long val;
  669. unsigned long flags;
  670. int ret;
  671. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  672. SLINK_CS_POLARITY,
  673. SLINK_CS_POLARITY1,
  674. SLINK_CS_POLARITY2,
  675. SLINK_CS_POLARITY3,
  676. };
  677. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  678. spi->bits_per_word,
  679. spi->mode & SPI_CPOL ? "" : "~",
  680. spi->mode & SPI_CPHA ? "" : "~",
  681. spi->max_speed_hz);
  682. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  683. /* Set speed to the spi max fequency if spi device has not set */
  684. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  685. ret = pm_runtime_get_sync(tspi->dev);
  686. if (ret < 0) {
  687. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  688. return ret;
  689. }
  690. spin_lock_irqsave(&tspi->lock, flags);
  691. val = tspi->def_command_reg;
  692. if (spi->mode & SPI_CS_HIGH)
  693. val |= cs_pol_bit[spi->chip_select];
  694. else
  695. val &= ~cs_pol_bit[spi->chip_select];
  696. tspi->def_command_reg = val;
  697. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  698. spin_unlock_irqrestore(&tspi->lock, flags);
  699. pm_runtime_put(tspi->dev);
  700. return 0;
  701. }
  702. static int tegra_slink_prepare_message(struct spi_master *master,
  703. struct spi_message *msg)
  704. {
  705. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  706. struct spi_device *spi = msg->spi;
  707. tegra_slink_clear_status(tspi);
  708. tspi->command_reg = tspi->def_command_reg;
  709. tspi->command_reg |= SLINK_CS_SW | SLINK_CS_VALUE;
  710. tspi->command2_reg = tspi->def_command2_reg;
  711. tspi->command2_reg |= SLINK_SS_EN_CS(spi->chip_select);
  712. tspi->command_reg &= ~SLINK_MODES;
  713. if (spi->mode & SPI_CPHA)
  714. tspi->command_reg |= SLINK_CK_SDA;
  715. if (spi->mode & SPI_CPOL)
  716. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  717. else
  718. tspi->command_reg |= SLINK_IDLE_SCLK_DRIVE_LOW;
  719. return 0;
  720. }
  721. static int tegra_slink_transfer_one(struct spi_master *master,
  722. struct spi_device *spi,
  723. struct spi_transfer *xfer)
  724. {
  725. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  726. int ret;
  727. INIT_COMPLETION(tspi->xfer_completion);
  728. ret = tegra_slink_start_transfer_one(spi, xfer);
  729. if (ret < 0) {
  730. dev_err(tspi->dev,
  731. "spi can not start transfer, err %d\n", ret);
  732. return ret;
  733. }
  734. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  735. SLINK_DMA_TIMEOUT);
  736. if (WARN_ON(ret == 0)) {
  737. dev_err(tspi->dev,
  738. "spi trasfer timeout, err %d\n", ret);
  739. return -EIO;
  740. }
  741. if (tspi->tx_status)
  742. return tspi->tx_status;
  743. if (tspi->rx_status)
  744. return tspi->rx_status;
  745. return 0;
  746. }
  747. static int tegra_slink_unprepare_message(struct spi_master *master,
  748. struct spi_message *msg)
  749. {
  750. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  751. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  752. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  753. return 0;
  754. }
  755. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  756. {
  757. struct spi_transfer *t = tspi->curr_xfer;
  758. unsigned long flags;
  759. spin_lock_irqsave(&tspi->lock, flags);
  760. if (tspi->tx_status || tspi->rx_status ||
  761. (tspi->status_reg & SLINK_BSY)) {
  762. dev_err(tspi->dev,
  763. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  764. dev_err(tspi->dev,
  765. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  766. tspi->command2_reg, tspi->dma_control_reg);
  767. tegra_periph_reset_assert(tspi->clk);
  768. udelay(2);
  769. tegra_periph_reset_deassert(tspi->clk);
  770. complete(&tspi->xfer_completion);
  771. goto exit;
  772. }
  773. if (tspi->cur_direction & DATA_DIR_RX)
  774. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  775. if (tspi->cur_direction & DATA_DIR_TX)
  776. tspi->cur_pos = tspi->cur_tx_pos;
  777. else
  778. tspi->cur_pos = tspi->cur_rx_pos;
  779. if (tspi->cur_pos == t->len) {
  780. complete(&tspi->xfer_completion);
  781. goto exit;
  782. }
  783. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  784. tegra_slink_start_cpu_based_transfer(tspi, t);
  785. exit:
  786. spin_unlock_irqrestore(&tspi->lock, flags);
  787. return IRQ_HANDLED;
  788. }
  789. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  790. {
  791. struct spi_transfer *t = tspi->curr_xfer;
  792. long wait_status;
  793. int err = 0;
  794. unsigned total_fifo_words;
  795. unsigned long flags;
  796. /* Abort dmas if any error */
  797. if (tspi->cur_direction & DATA_DIR_TX) {
  798. if (tspi->tx_status) {
  799. dmaengine_terminate_all(tspi->tx_dma_chan);
  800. err += 1;
  801. } else {
  802. wait_status = wait_for_completion_interruptible_timeout(
  803. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  804. if (wait_status <= 0) {
  805. dmaengine_terminate_all(tspi->tx_dma_chan);
  806. dev_err(tspi->dev, "TxDma Xfer failed\n");
  807. err += 1;
  808. }
  809. }
  810. }
  811. if (tspi->cur_direction & DATA_DIR_RX) {
  812. if (tspi->rx_status) {
  813. dmaengine_terminate_all(tspi->rx_dma_chan);
  814. err += 2;
  815. } else {
  816. wait_status = wait_for_completion_interruptible_timeout(
  817. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  818. if (wait_status <= 0) {
  819. dmaengine_terminate_all(tspi->rx_dma_chan);
  820. dev_err(tspi->dev, "RxDma Xfer failed\n");
  821. err += 2;
  822. }
  823. }
  824. }
  825. spin_lock_irqsave(&tspi->lock, flags);
  826. if (err) {
  827. dev_err(tspi->dev,
  828. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  829. dev_err(tspi->dev,
  830. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  831. tspi->command2_reg, tspi->dma_control_reg);
  832. tegra_periph_reset_assert(tspi->clk);
  833. udelay(2);
  834. tegra_periph_reset_deassert(tspi->clk);
  835. complete(&tspi->xfer_completion);
  836. spin_unlock_irqrestore(&tspi->lock, flags);
  837. return IRQ_HANDLED;
  838. }
  839. if (tspi->cur_direction & DATA_DIR_RX)
  840. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  841. if (tspi->cur_direction & DATA_DIR_TX)
  842. tspi->cur_pos = tspi->cur_tx_pos;
  843. else
  844. tspi->cur_pos = tspi->cur_rx_pos;
  845. if (tspi->cur_pos == t->len) {
  846. complete(&tspi->xfer_completion);
  847. goto exit;
  848. }
  849. /* Continue transfer in current message */
  850. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  851. tspi, t);
  852. if (total_fifo_words > SLINK_FIFO_DEPTH)
  853. err = tegra_slink_start_dma_based_transfer(tspi, t);
  854. else
  855. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  856. exit:
  857. spin_unlock_irqrestore(&tspi->lock, flags);
  858. return IRQ_HANDLED;
  859. }
  860. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  861. {
  862. struct tegra_slink_data *tspi = context_data;
  863. if (!tspi->is_curr_dma_xfer)
  864. return handle_cpu_based_xfer(tspi);
  865. return handle_dma_based_xfer(tspi);
  866. }
  867. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  868. {
  869. struct tegra_slink_data *tspi = context_data;
  870. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  871. if (tspi->cur_direction & DATA_DIR_TX)
  872. tspi->tx_status = tspi->status_reg &
  873. (SLINK_TX_OVF | SLINK_TX_UNF);
  874. if (tspi->cur_direction & DATA_DIR_RX)
  875. tspi->rx_status = tspi->status_reg &
  876. (SLINK_RX_OVF | SLINK_RX_UNF);
  877. tegra_slink_clear_status(tspi);
  878. return IRQ_WAKE_THREAD;
  879. }
  880. static void tegra_slink_parse_dt(struct tegra_slink_data *tspi)
  881. {
  882. struct device_node *np = tspi->dev->of_node;
  883. u32 of_dma[2];
  884. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  885. of_dma, 2) >= 0)
  886. tspi->dma_req_sel = of_dma[1];
  887. if (of_property_read_u32(np, "spi-max-frequency",
  888. &tspi->spi_max_frequency))
  889. tspi->spi_max_frequency = 25000000; /* 25MHz */
  890. }
  891. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  892. .cs_hold_time = true,
  893. };
  894. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  895. .cs_hold_time = false,
  896. };
  897. static struct of_device_id tegra_slink_of_match[] = {
  898. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  899. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  900. {}
  901. };
  902. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  903. static int tegra_slink_probe(struct platform_device *pdev)
  904. {
  905. struct spi_master *master;
  906. struct tegra_slink_data *tspi;
  907. struct resource *r;
  908. int ret, spi_irq;
  909. const struct tegra_slink_chip_data *cdata = NULL;
  910. const struct of_device_id *match;
  911. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  912. if (!match) {
  913. dev_err(&pdev->dev, "Error: No device match found\n");
  914. return -ENODEV;
  915. }
  916. cdata = match->data;
  917. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  918. if (!master) {
  919. dev_err(&pdev->dev, "master allocation failed\n");
  920. return -ENOMEM;
  921. }
  922. /* the spi->mode bits understood by this driver: */
  923. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  924. master->setup = tegra_slink_setup;
  925. master->prepare_message = tegra_slink_prepare_message;
  926. master->transfer_one = tegra_slink_transfer_one;
  927. master->unprepare_message = tegra_slink_unprepare_message;
  928. master->auto_runtime_pm = true;
  929. master->num_chipselect = MAX_CHIP_SELECT;
  930. master->bus_num = -1;
  931. platform_set_drvdata(pdev, master);
  932. tspi = spi_master_get_devdata(master);
  933. tspi->master = master;
  934. tspi->dev = &pdev->dev;
  935. tspi->chip_data = cdata;
  936. spin_lock_init(&tspi->lock);
  937. tegra_slink_parse_dt(tspi);
  938. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  939. if (!r) {
  940. dev_err(&pdev->dev, "No IO memory resource\n");
  941. ret = -ENODEV;
  942. goto exit_free_master;
  943. }
  944. tspi->phys = r->start;
  945. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  946. if (IS_ERR(tspi->base)) {
  947. ret = PTR_ERR(tspi->base);
  948. goto exit_free_master;
  949. }
  950. spi_irq = platform_get_irq(pdev, 0);
  951. tspi->irq = spi_irq;
  952. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  953. tegra_slink_isr_thread, IRQF_ONESHOT,
  954. dev_name(&pdev->dev), tspi);
  955. if (ret < 0) {
  956. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  957. tspi->irq);
  958. goto exit_free_master;
  959. }
  960. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  961. if (IS_ERR(tspi->clk)) {
  962. dev_err(&pdev->dev, "can not get clock\n");
  963. ret = PTR_ERR(tspi->clk);
  964. goto exit_free_irq;
  965. }
  966. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  967. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  968. if (tspi->dma_req_sel) {
  969. ret = tegra_slink_init_dma_param(tspi, true);
  970. if (ret < 0) {
  971. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  972. goto exit_free_irq;
  973. }
  974. ret = tegra_slink_init_dma_param(tspi, false);
  975. if (ret < 0) {
  976. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  977. goto exit_rx_dma_free;
  978. }
  979. tspi->max_buf_size = tspi->dma_buf_size;
  980. init_completion(&tspi->tx_dma_complete);
  981. init_completion(&tspi->rx_dma_complete);
  982. }
  983. init_completion(&tspi->xfer_completion);
  984. pm_runtime_enable(&pdev->dev);
  985. if (!pm_runtime_enabled(&pdev->dev)) {
  986. ret = tegra_slink_runtime_resume(&pdev->dev);
  987. if (ret)
  988. goto exit_pm_disable;
  989. }
  990. ret = pm_runtime_get_sync(&pdev->dev);
  991. if (ret < 0) {
  992. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  993. goto exit_pm_disable;
  994. }
  995. tspi->def_command_reg = SLINK_M_S;
  996. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  997. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  998. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  999. pm_runtime_put(&pdev->dev);
  1000. master->dev.of_node = pdev->dev.of_node;
  1001. ret = devm_spi_register_master(&pdev->dev, master);
  1002. if (ret < 0) {
  1003. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1004. goto exit_pm_disable;
  1005. }
  1006. return ret;
  1007. exit_pm_disable:
  1008. pm_runtime_disable(&pdev->dev);
  1009. if (!pm_runtime_status_suspended(&pdev->dev))
  1010. tegra_slink_runtime_suspend(&pdev->dev);
  1011. tegra_slink_deinit_dma_param(tspi, false);
  1012. exit_rx_dma_free:
  1013. tegra_slink_deinit_dma_param(tspi, true);
  1014. exit_free_irq:
  1015. free_irq(spi_irq, tspi);
  1016. exit_free_master:
  1017. spi_master_put(master);
  1018. return ret;
  1019. }
  1020. static int tegra_slink_remove(struct platform_device *pdev)
  1021. {
  1022. struct spi_master *master = platform_get_drvdata(pdev);
  1023. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1024. free_irq(tspi->irq, tspi);
  1025. if (tspi->tx_dma_chan)
  1026. tegra_slink_deinit_dma_param(tspi, false);
  1027. if (tspi->rx_dma_chan)
  1028. tegra_slink_deinit_dma_param(tspi, true);
  1029. pm_runtime_disable(&pdev->dev);
  1030. if (!pm_runtime_status_suspended(&pdev->dev))
  1031. tegra_slink_runtime_suspend(&pdev->dev);
  1032. return 0;
  1033. }
  1034. #ifdef CONFIG_PM_SLEEP
  1035. static int tegra_slink_suspend(struct device *dev)
  1036. {
  1037. struct spi_master *master = dev_get_drvdata(dev);
  1038. return spi_master_suspend(master);
  1039. }
  1040. static int tegra_slink_resume(struct device *dev)
  1041. {
  1042. struct spi_master *master = dev_get_drvdata(dev);
  1043. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1044. int ret;
  1045. ret = pm_runtime_get_sync(dev);
  1046. if (ret < 0) {
  1047. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1048. return ret;
  1049. }
  1050. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1051. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1052. pm_runtime_put(dev);
  1053. return spi_master_resume(master);
  1054. }
  1055. #endif
  1056. static int tegra_slink_runtime_suspend(struct device *dev)
  1057. {
  1058. struct spi_master *master = dev_get_drvdata(dev);
  1059. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1060. /* Flush all write which are in PPSB queue by reading back */
  1061. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1062. clk_disable_unprepare(tspi->clk);
  1063. return 0;
  1064. }
  1065. static int tegra_slink_runtime_resume(struct device *dev)
  1066. {
  1067. struct spi_master *master = dev_get_drvdata(dev);
  1068. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1069. int ret;
  1070. ret = clk_prepare_enable(tspi->clk);
  1071. if (ret < 0) {
  1072. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1073. return ret;
  1074. }
  1075. return 0;
  1076. }
  1077. static const struct dev_pm_ops slink_pm_ops = {
  1078. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1079. tegra_slink_runtime_resume, NULL)
  1080. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1081. };
  1082. static struct platform_driver tegra_slink_driver = {
  1083. .driver = {
  1084. .name = "spi-tegra-slink",
  1085. .owner = THIS_MODULE,
  1086. .pm = &slink_pm_ops,
  1087. .of_match_table = tegra_slink_of_match,
  1088. },
  1089. .probe = tegra_slink_probe,
  1090. .remove = tegra_slink_remove,
  1091. };
  1092. module_platform_driver(tegra_slink_driver);
  1093. MODULE_ALIAS("platform:spi-tegra-slink");
  1094. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1095. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1096. MODULE_LICENSE("GPL v2");