spi-tegra20-slink.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  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 = (bits_per_word - 1) / 8 + 1;
  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, bool is_first_of_msg,
  621. bool is_single_xfer)
  622. {
  623. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  624. u32 speed;
  625. u8 bits_per_word;
  626. unsigned total_fifo_words;
  627. int ret;
  628. unsigned long command;
  629. unsigned long command2;
  630. bits_per_word = t->bits_per_word;
  631. speed = t->speed_hz;
  632. if (speed != tspi->cur_speed) {
  633. clk_set_rate(tspi->clk, speed * 4);
  634. tspi->cur_speed = speed;
  635. }
  636. tspi->cur_spi = spi;
  637. tspi->cur_pos = 0;
  638. tspi->cur_rx_pos = 0;
  639. tspi->cur_tx_pos = 0;
  640. tspi->curr_xfer = t;
  641. total_fifo_words = tegra_slink_calculate_curr_xfer_param(spi, tspi, t);
  642. if (is_first_of_msg) {
  643. tegra_slink_clear_status(tspi);
  644. command = tspi->def_command_reg;
  645. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  646. command |= SLINK_CS_SW | SLINK_CS_VALUE;
  647. command2 = tspi->def_command2_reg;
  648. command2 |= SLINK_SS_EN_CS(spi->chip_select);
  649. command &= ~SLINK_MODES;
  650. if (spi->mode & SPI_CPHA)
  651. command |= SLINK_CK_SDA;
  652. if (spi->mode & SPI_CPOL)
  653. command |= SLINK_IDLE_SCLK_DRIVE_HIGH;
  654. else
  655. command |= SLINK_IDLE_SCLK_DRIVE_LOW;
  656. } else {
  657. command = tspi->command_reg;
  658. command &= ~SLINK_BIT_LENGTH(~0);
  659. command |= SLINK_BIT_LENGTH(bits_per_word - 1);
  660. command2 = tspi->command2_reg;
  661. command2 &= ~(SLINK_RXEN | SLINK_TXEN);
  662. }
  663. tegra_slink_writel(tspi, command, SLINK_COMMAND);
  664. tspi->command_reg = command;
  665. tspi->cur_direction = 0;
  666. if (t->rx_buf) {
  667. command2 |= SLINK_RXEN;
  668. tspi->cur_direction |= DATA_DIR_RX;
  669. }
  670. if (t->tx_buf) {
  671. command2 |= SLINK_TXEN;
  672. tspi->cur_direction |= DATA_DIR_TX;
  673. }
  674. tegra_slink_writel(tspi, command2, SLINK_COMMAND2);
  675. tspi->command2_reg = command2;
  676. if (total_fifo_words > SLINK_FIFO_DEPTH)
  677. ret = tegra_slink_start_dma_based_transfer(tspi, t);
  678. else
  679. ret = tegra_slink_start_cpu_based_transfer(tspi, t);
  680. return ret;
  681. }
  682. static int tegra_slink_setup(struct spi_device *spi)
  683. {
  684. struct tegra_slink_data *tspi = spi_master_get_devdata(spi->master);
  685. unsigned long val;
  686. unsigned long flags;
  687. int ret;
  688. unsigned int cs_pol_bit[MAX_CHIP_SELECT] = {
  689. SLINK_CS_POLARITY,
  690. SLINK_CS_POLARITY1,
  691. SLINK_CS_POLARITY2,
  692. SLINK_CS_POLARITY3,
  693. };
  694. dev_dbg(&spi->dev, "setup %d bpw, %scpol, %scpha, %dHz\n",
  695. spi->bits_per_word,
  696. spi->mode & SPI_CPOL ? "" : "~",
  697. spi->mode & SPI_CPHA ? "" : "~",
  698. spi->max_speed_hz);
  699. BUG_ON(spi->chip_select >= MAX_CHIP_SELECT);
  700. /* Set speed to the spi max fequency if spi device has not set */
  701. spi->max_speed_hz = spi->max_speed_hz ? : tspi->spi_max_frequency;
  702. ret = pm_runtime_get_sync(tspi->dev);
  703. if (ret < 0) {
  704. dev_err(tspi->dev, "pm runtime failed, e = %d\n", ret);
  705. return ret;
  706. }
  707. spin_lock_irqsave(&tspi->lock, flags);
  708. val = tspi->def_command_reg;
  709. if (spi->mode & SPI_CS_HIGH)
  710. val |= cs_pol_bit[spi->chip_select];
  711. else
  712. val &= ~cs_pol_bit[spi->chip_select];
  713. tspi->def_command_reg = val;
  714. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  715. spin_unlock_irqrestore(&tspi->lock, flags);
  716. pm_runtime_put(tspi->dev);
  717. return 0;
  718. }
  719. static int tegra_slink_transfer_one_message(struct spi_master *master,
  720. struct spi_message *msg)
  721. {
  722. bool is_first_msg = true;
  723. int single_xfer;
  724. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  725. struct spi_transfer *xfer;
  726. struct spi_device *spi = msg->spi;
  727. int ret;
  728. msg->status = 0;
  729. msg->actual_length = 0;
  730. ret = pm_runtime_get_sync(tspi->dev);
  731. if (ret < 0) {
  732. dev_err(tspi->dev, "runtime get failed: %d\n", ret);
  733. goto done;
  734. }
  735. single_xfer = list_is_singular(&msg->transfers);
  736. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  737. INIT_COMPLETION(tspi->xfer_completion);
  738. ret = tegra_slink_start_transfer_one(spi, xfer,
  739. is_first_msg, single_xfer);
  740. if (ret < 0) {
  741. dev_err(tspi->dev,
  742. "spi can not start transfer, err %d\n", ret);
  743. goto exit;
  744. }
  745. is_first_msg = false;
  746. ret = wait_for_completion_timeout(&tspi->xfer_completion,
  747. SLINK_DMA_TIMEOUT);
  748. if (WARN_ON(ret == 0)) {
  749. dev_err(tspi->dev,
  750. "spi trasfer timeout, err %d\n", ret);
  751. ret = -EIO;
  752. goto exit;
  753. }
  754. if (tspi->tx_status || tspi->rx_status) {
  755. dev_err(tspi->dev, "Error in Transfer\n");
  756. ret = -EIO;
  757. goto exit;
  758. }
  759. msg->actual_length += xfer->len;
  760. if (xfer->cs_change && xfer->delay_usecs) {
  761. tegra_slink_writel(tspi, tspi->def_command_reg,
  762. SLINK_COMMAND);
  763. udelay(xfer->delay_usecs);
  764. }
  765. }
  766. ret = 0;
  767. exit:
  768. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  769. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  770. pm_runtime_put(tspi->dev);
  771. done:
  772. msg->status = ret;
  773. spi_finalize_current_message(master);
  774. return ret;
  775. }
  776. static irqreturn_t handle_cpu_based_xfer(struct tegra_slink_data *tspi)
  777. {
  778. struct spi_transfer *t = tspi->curr_xfer;
  779. unsigned long flags;
  780. spin_lock_irqsave(&tspi->lock, flags);
  781. if (tspi->tx_status || tspi->rx_status ||
  782. (tspi->status_reg & SLINK_BSY)) {
  783. dev_err(tspi->dev,
  784. "CpuXfer ERROR bit set 0x%x\n", tspi->status_reg);
  785. dev_err(tspi->dev,
  786. "CpuXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  787. tspi->command2_reg, tspi->dma_control_reg);
  788. tegra_periph_reset_assert(tspi->clk);
  789. udelay(2);
  790. tegra_periph_reset_deassert(tspi->clk);
  791. complete(&tspi->xfer_completion);
  792. goto exit;
  793. }
  794. if (tspi->cur_direction & DATA_DIR_RX)
  795. tegra_slink_read_rx_fifo_to_client_rxbuf(tspi, t);
  796. if (tspi->cur_direction & DATA_DIR_TX)
  797. tspi->cur_pos = tspi->cur_tx_pos;
  798. else
  799. tspi->cur_pos = tspi->cur_rx_pos;
  800. if (tspi->cur_pos == t->len) {
  801. complete(&tspi->xfer_completion);
  802. goto exit;
  803. }
  804. tegra_slink_calculate_curr_xfer_param(tspi->cur_spi, tspi, t);
  805. tegra_slink_start_cpu_based_transfer(tspi, t);
  806. exit:
  807. spin_unlock_irqrestore(&tspi->lock, flags);
  808. return IRQ_HANDLED;
  809. }
  810. static irqreturn_t handle_dma_based_xfer(struct tegra_slink_data *tspi)
  811. {
  812. struct spi_transfer *t = tspi->curr_xfer;
  813. long wait_status;
  814. int err = 0;
  815. unsigned total_fifo_words;
  816. unsigned long flags;
  817. /* Abort dmas if any error */
  818. if (tspi->cur_direction & DATA_DIR_TX) {
  819. if (tspi->tx_status) {
  820. dmaengine_terminate_all(tspi->tx_dma_chan);
  821. err += 1;
  822. } else {
  823. wait_status = wait_for_completion_interruptible_timeout(
  824. &tspi->tx_dma_complete, SLINK_DMA_TIMEOUT);
  825. if (wait_status <= 0) {
  826. dmaengine_terminate_all(tspi->tx_dma_chan);
  827. dev_err(tspi->dev, "TxDma Xfer failed\n");
  828. err += 1;
  829. }
  830. }
  831. }
  832. if (tspi->cur_direction & DATA_DIR_RX) {
  833. if (tspi->rx_status) {
  834. dmaengine_terminate_all(tspi->rx_dma_chan);
  835. err += 2;
  836. } else {
  837. wait_status = wait_for_completion_interruptible_timeout(
  838. &tspi->rx_dma_complete, SLINK_DMA_TIMEOUT);
  839. if (wait_status <= 0) {
  840. dmaengine_terminate_all(tspi->rx_dma_chan);
  841. dev_err(tspi->dev, "RxDma Xfer failed\n");
  842. err += 2;
  843. }
  844. }
  845. }
  846. spin_lock_irqsave(&tspi->lock, flags);
  847. if (err) {
  848. dev_err(tspi->dev,
  849. "DmaXfer: ERROR bit set 0x%x\n", tspi->status_reg);
  850. dev_err(tspi->dev,
  851. "DmaXfer 0x%08x:0x%08x:0x%08x\n", tspi->command_reg,
  852. tspi->command2_reg, tspi->dma_control_reg);
  853. tegra_periph_reset_assert(tspi->clk);
  854. udelay(2);
  855. tegra_periph_reset_deassert(tspi->clk);
  856. complete(&tspi->xfer_completion);
  857. spin_unlock_irqrestore(&tspi->lock, flags);
  858. return IRQ_HANDLED;
  859. }
  860. if (tspi->cur_direction & DATA_DIR_RX)
  861. tegra_slink_copy_spi_rxbuf_to_client_rxbuf(tspi, t);
  862. if (tspi->cur_direction & DATA_DIR_TX)
  863. tspi->cur_pos = tspi->cur_tx_pos;
  864. else
  865. tspi->cur_pos = tspi->cur_rx_pos;
  866. if (tspi->cur_pos == t->len) {
  867. complete(&tspi->xfer_completion);
  868. goto exit;
  869. }
  870. /* Continue transfer in current message */
  871. total_fifo_words = tegra_slink_calculate_curr_xfer_param(tspi->cur_spi,
  872. tspi, t);
  873. if (total_fifo_words > SLINK_FIFO_DEPTH)
  874. err = tegra_slink_start_dma_based_transfer(tspi, t);
  875. else
  876. err = tegra_slink_start_cpu_based_transfer(tspi, t);
  877. exit:
  878. spin_unlock_irqrestore(&tspi->lock, flags);
  879. return IRQ_HANDLED;
  880. }
  881. static irqreturn_t tegra_slink_isr_thread(int irq, void *context_data)
  882. {
  883. struct tegra_slink_data *tspi = context_data;
  884. if (!tspi->is_curr_dma_xfer)
  885. return handle_cpu_based_xfer(tspi);
  886. return handle_dma_based_xfer(tspi);
  887. }
  888. static irqreturn_t tegra_slink_isr(int irq, void *context_data)
  889. {
  890. struct tegra_slink_data *tspi = context_data;
  891. tspi->status_reg = tegra_slink_readl(tspi, SLINK_STATUS);
  892. if (tspi->cur_direction & DATA_DIR_TX)
  893. tspi->tx_status = tspi->status_reg &
  894. (SLINK_TX_OVF | SLINK_TX_UNF);
  895. if (tspi->cur_direction & DATA_DIR_RX)
  896. tspi->rx_status = tspi->status_reg &
  897. (SLINK_RX_OVF | SLINK_RX_UNF);
  898. tegra_slink_clear_status(tspi);
  899. return IRQ_WAKE_THREAD;
  900. }
  901. static void tegra_slink_parse_dt(struct tegra_slink_data *tspi)
  902. {
  903. struct device_node *np = tspi->dev->of_node;
  904. u32 of_dma[2];
  905. if (of_property_read_u32_array(np, "nvidia,dma-request-selector",
  906. of_dma, 2) >= 0)
  907. tspi->dma_req_sel = of_dma[1];
  908. if (of_property_read_u32(np, "spi-max-frequency",
  909. &tspi->spi_max_frequency))
  910. tspi->spi_max_frequency = 25000000; /* 25MHz */
  911. }
  912. static const struct tegra_slink_chip_data tegra30_spi_cdata = {
  913. .cs_hold_time = true,
  914. };
  915. static const struct tegra_slink_chip_data tegra20_spi_cdata = {
  916. .cs_hold_time = false,
  917. };
  918. static struct of_device_id tegra_slink_of_match[] = {
  919. { .compatible = "nvidia,tegra30-slink", .data = &tegra30_spi_cdata, },
  920. { .compatible = "nvidia,tegra20-slink", .data = &tegra20_spi_cdata, },
  921. {}
  922. };
  923. MODULE_DEVICE_TABLE(of, tegra_slink_of_match);
  924. static int tegra_slink_probe(struct platform_device *pdev)
  925. {
  926. struct spi_master *master;
  927. struct tegra_slink_data *tspi;
  928. struct resource *r;
  929. int ret, spi_irq;
  930. const struct tegra_slink_chip_data *cdata = NULL;
  931. const struct of_device_id *match;
  932. match = of_match_device(tegra_slink_of_match, &pdev->dev);
  933. if (!match) {
  934. dev_err(&pdev->dev, "Error: No device match found\n");
  935. return -ENODEV;
  936. }
  937. cdata = match->data;
  938. master = spi_alloc_master(&pdev->dev, sizeof(*tspi));
  939. if (!master) {
  940. dev_err(&pdev->dev, "master allocation failed\n");
  941. return -ENOMEM;
  942. }
  943. /* the spi->mode bits understood by this driver: */
  944. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  945. master->setup = tegra_slink_setup;
  946. master->transfer_one_message = tegra_slink_transfer_one_message;
  947. master->num_chipselect = MAX_CHIP_SELECT;
  948. master->bus_num = -1;
  949. dev_set_drvdata(&pdev->dev, master);
  950. tspi = spi_master_get_devdata(master);
  951. tspi->master = master;
  952. tspi->dev = &pdev->dev;
  953. tspi->chip_data = cdata;
  954. spin_lock_init(&tspi->lock);
  955. tegra_slink_parse_dt(tspi);
  956. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  957. if (!r) {
  958. dev_err(&pdev->dev, "No IO memory resource\n");
  959. ret = -ENODEV;
  960. goto exit_free_master;
  961. }
  962. tspi->phys = r->start;
  963. tspi->base = devm_ioremap_resource(&pdev->dev, r);
  964. if (IS_ERR(tspi->base)) {
  965. ret = PTR_ERR(tspi->base);
  966. goto exit_free_master;
  967. }
  968. spi_irq = platform_get_irq(pdev, 0);
  969. tspi->irq = spi_irq;
  970. ret = request_threaded_irq(tspi->irq, tegra_slink_isr,
  971. tegra_slink_isr_thread, IRQF_ONESHOT,
  972. dev_name(&pdev->dev), tspi);
  973. if (ret < 0) {
  974. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  975. tspi->irq);
  976. goto exit_free_master;
  977. }
  978. tspi->clk = devm_clk_get(&pdev->dev, NULL);
  979. if (IS_ERR(tspi->clk)) {
  980. dev_err(&pdev->dev, "can not get clock\n");
  981. ret = PTR_ERR(tspi->clk);
  982. goto exit_free_irq;
  983. }
  984. tspi->max_buf_size = SLINK_FIFO_DEPTH << 2;
  985. tspi->dma_buf_size = DEFAULT_SPI_DMA_BUF_LEN;
  986. if (tspi->dma_req_sel) {
  987. ret = tegra_slink_init_dma_param(tspi, true);
  988. if (ret < 0) {
  989. dev_err(&pdev->dev, "RxDma Init failed, err %d\n", ret);
  990. goto exit_free_irq;
  991. }
  992. ret = tegra_slink_init_dma_param(tspi, false);
  993. if (ret < 0) {
  994. dev_err(&pdev->dev, "TxDma Init failed, err %d\n", ret);
  995. goto exit_rx_dma_free;
  996. }
  997. tspi->max_buf_size = tspi->dma_buf_size;
  998. init_completion(&tspi->tx_dma_complete);
  999. init_completion(&tspi->rx_dma_complete);
  1000. }
  1001. init_completion(&tspi->xfer_completion);
  1002. pm_runtime_enable(&pdev->dev);
  1003. if (!pm_runtime_enabled(&pdev->dev)) {
  1004. ret = tegra_slink_runtime_resume(&pdev->dev);
  1005. if (ret)
  1006. goto exit_pm_disable;
  1007. }
  1008. ret = pm_runtime_get_sync(&pdev->dev);
  1009. if (ret < 0) {
  1010. dev_err(&pdev->dev, "pm runtime get failed, e = %d\n", ret);
  1011. goto exit_pm_disable;
  1012. }
  1013. tspi->def_command_reg = SLINK_M_S;
  1014. tspi->def_command2_reg = SLINK_CS_ACTIVE_BETWEEN;
  1015. tegra_slink_writel(tspi, tspi->def_command_reg, SLINK_COMMAND);
  1016. tegra_slink_writel(tspi, tspi->def_command2_reg, SLINK_COMMAND2);
  1017. pm_runtime_put(&pdev->dev);
  1018. master->dev.of_node = pdev->dev.of_node;
  1019. ret = spi_register_master(master);
  1020. if (ret < 0) {
  1021. dev_err(&pdev->dev, "can not register to master err %d\n", ret);
  1022. goto exit_pm_disable;
  1023. }
  1024. return ret;
  1025. exit_pm_disable:
  1026. pm_runtime_disable(&pdev->dev);
  1027. if (!pm_runtime_status_suspended(&pdev->dev))
  1028. tegra_slink_runtime_suspend(&pdev->dev);
  1029. tegra_slink_deinit_dma_param(tspi, false);
  1030. exit_rx_dma_free:
  1031. tegra_slink_deinit_dma_param(tspi, true);
  1032. exit_free_irq:
  1033. free_irq(spi_irq, tspi);
  1034. exit_free_master:
  1035. spi_master_put(master);
  1036. return ret;
  1037. }
  1038. static int tegra_slink_remove(struct platform_device *pdev)
  1039. {
  1040. struct spi_master *master = dev_get_drvdata(&pdev->dev);
  1041. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1042. free_irq(tspi->irq, tspi);
  1043. spi_unregister_master(master);
  1044. if (tspi->tx_dma_chan)
  1045. tegra_slink_deinit_dma_param(tspi, false);
  1046. if (tspi->rx_dma_chan)
  1047. tegra_slink_deinit_dma_param(tspi, true);
  1048. pm_runtime_disable(&pdev->dev);
  1049. if (!pm_runtime_status_suspended(&pdev->dev))
  1050. tegra_slink_runtime_suspend(&pdev->dev);
  1051. return 0;
  1052. }
  1053. #ifdef CONFIG_PM_SLEEP
  1054. static int tegra_slink_suspend(struct device *dev)
  1055. {
  1056. struct spi_master *master = dev_get_drvdata(dev);
  1057. return spi_master_suspend(master);
  1058. }
  1059. static int tegra_slink_resume(struct device *dev)
  1060. {
  1061. struct spi_master *master = dev_get_drvdata(dev);
  1062. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1063. int ret;
  1064. ret = pm_runtime_get_sync(dev);
  1065. if (ret < 0) {
  1066. dev_err(dev, "pm runtime failed, e = %d\n", ret);
  1067. return ret;
  1068. }
  1069. tegra_slink_writel(tspi, tspi->command_reg, SLINK_COMMAND);
  1070. tegra_slink_writel(tspi, tspi->command2_reg, SLINK_COMMAND2);
  1071. pm_runtime_put(dev);
  1072. return spi_master_resume(master);
  1073. }
  1074. #endif
  1075. static int tegra_slink_runtime_suspend(struct device *dev)
  1076. {
  1077. struct spi_master *master = dev_get_drvdata(dev);
  1078. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1079. /* Flush all write which are in PPSB queue by reading back */
  1080. tegra_slink_readl(tspi, SLINK_MAS_DATA);
  1081. clk_disable_unprepare(tspi->clk);
  1082. return 0;
  1083. }
  1084. static int tegra_slink_runtime_resume(struct device *dev)
  1085. {
  1086. struct spi_master *master = dev_get_drvdata(dev);
  1087. struct tegra_slink_data *tspi = spi_master_get_devdata(master);
  1088. int ret;
  1089. ret = clk_prepare_enable(tspi->clk);
  1090. if (ret < 0) {
  1091. dev_err(tspi->dev, "clk_prepare failed: %d\n", ret);
  1092. return ret;
  1093. }
  1094. return 0;
  1095. }
  1096. static const struct dev_pm_ops slink_pm_ops = {
  1097. SET_RUNTIME_PM_OPS(tegra_slink_runtime_suspend,
  1098. tegra_slink_runtime_resume, NULL)
  1099. SET_SYSTEM_SLEEP_PM_OPS(tegra_slink_suspend, tegra_slink_resume)
  1100. };
  1101. static struct platform_driver tegra_slink_driver = {
  1102. .driver = {
  1103. .name = "spi-tegra-slink",
  1104. .owner = THIS_MODULE,
  1105. .pm = &slink_pm_ops,
  1106. .of_match_table = tegra_slink_of_match,
  1107. },
  1108. .probe = tegra_slink_probe,
  1109. .remove = tegra_slink_remove,
  1110. };
  1111. module_platform_driver(tegra_slink_driver);
  1112. MODULE_ALIAS("platform:spi-tegra-slink");
  1113. MODULE_DESCRIPTION("NVIDIA Tegra20/Tegra30 SLINK Controller Driver");
  1114. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  1115. MODULE_LICENSE("GPL v2");