150-mmc-octeon-add-host-driver-for-octeon-mmc-controller.patch 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622
  1. --- /dev/null
  2. +++ b/Documentation/devicetree/bindings/mmc/octeon-mmc.txt
  3. @@ -0,0 +1,69 @@
  4. +* OCTEON SD/MMC Host Controller
  5. +
  6. +This controller is present on some members of the Cavium OCTEON SoC
  7. +family, provide an interface for eMMC, MMC and SD devices. There is a
  8. +single controller that may have several "slots" connected. These
  9. +slots appear as children of the main controller node.
  10. +The DMA engine is an integral part of the controller block.
  11. +
  12. +Required properties:
  13. +- compatible : Should be "cavium,octeon-6130-mmc" or "cavium,octeon-7890-mmc"
  14. +- reg : Two entries:
  15. + 1) The base address of the MMC controller register bank.
  16. + 2) The base address of the MMC DMA engine register bank.
  17. +- interrupts :
  18. + For "cavium,octeon-6130-mmc": two entries:
  19. + 1) The MMC controller interrupt line.
  20. + 2) The MMC DMA engine interrupt line.
  21. + For "cavium,octeon-7890-mmc": nine entries:
  22. + 1) The next block transfer of a multiblock transfer has completed (BUF_DONE)
  23. + 2) Operation completed successfully (CMD_DONE).
  24. + 3) DMA transfer completed successfully (DMA_DONE).
  25. + 4) Operation encountered an error (CMD_ERR).
  26. + 5) DMA transfer encountered an error (DMA_ERR).
  27. + 6) Switch operation completed successfully (SWITCH_DONE).
  28. + 7) Switch operation encountered an error (SWITCH_ERR).
  29. + 8) Internal DMA engine request completion interrupt (DONE).
  30. + 9) Internal DMA FIFO underflow (FIFO).
  31. +- #address-cells : Must be <1>
  32. +- #size-cells : Must be <0>
  33. +
  34. +Required properties of child nodes:
  35. +- compatible : Should be "cavium,octeon-6130-mmc-slot".
  36. +- reg : The slot number.
  37. +
  38. +Optional properties of child nodes:
  39. +- cd-gpios : Specify GPIOs for card detection
  40. +- wp-gpios : Specify GPIOs for write protection
  41. +- power-gpios : Specify GPIOs for power control
  42. +- cavium,bus-max-width : The number of data lines present in the slot.
  43. + Default is 8.
  44. +- spi-max-frequency : The maximum operating frequency of the slot.
  45. + Default is 52000000.
  46. +- cavium,cmd-clk-skew : the amount of delay (in pS) past the clock edge
  47. + to sample the command pin.
  48. +- cavium,dat-clk-skew : the amount of delay (in pS) past the clock edge
  49. + to sample the data pin.
  50. +
  51. +Example:
  52. + mmc@1180000002000 {
  53. + compatible = "cavium,octeon-6130-mmc";
  54. + reg = <0x11800 0x00002000 0x0 0x100>,
  55. + <0x11800 0x00000168 0x0 0x20>;
  56. + #address-cells = <1>;
  57. + #size-cells = <0>;
  58. + /* EMM irq, DMA irq */
  59. + interrupts = <1 19>, <0 63>;
  60. +
  61. + /* The board only has a single MMC slot */
  62. + mmc-slot@0 {
  63. + compatible = "cavium,octeon-6130-mmc-slot";
  64. + reg = <0>;
  65. + spi-max-frequency = <20000000>;
  66. + /* bus width can be 1, 4 or 8 */
  67. + cavium,bus-max-width = <8>;
  68. + cd-gpios = <&gpio 9 0>;
  69. + wp-gpios = <&gpio 10 0>;
  70. + power-gpios = <&gpio 8 0>;
  71. + };
  72. + };
  73. --- a/drivers/mmc/host/Kconfig
  74. +++ b/drivers/mmc/host/Kconfig
  75. @@ -436,6 +436,16 @@ config MMC_MXS
  76. If unsure, say N.
  77. +config MMC_OCTEON
  78. + tristate "Cavium OCTEON Multimedia Card Interface support"
  79. + depends on CAVIUM_OCTEON_SOC
  80. + help
  81. + This selects Cavium OCTEON Multimedia card Interface.
  82. + If you have an OCTEON board with a Multimedia Card slot,
  83. + say Y or M here.
  84. +
  85. + If unsure, say N.
  86. +
  87. config MMC_TIFM_SD
  88. tristate "TI Flash Media MMC/SD Interface support"
  89. depends on PCI
  90. --- a/drivers/mmc/host/Makefile
  91. +++ b/drivers/mmc/host/Makefile
  92. @@ -20,6 +20,7 @@ obj-$(CONFIG_MMC_SDHCI_F_SDH30) += sdhci
  93. obj-$(CONFIG_MMC_SDHCI_SPEAR) += sdhci-spear.o
  94. obj-$(CONFIG_MMC_WBSD) += wbsd.o
  95. obj-$(CONFIG_MMC_AU1X) += au1xmmc.o
  96. +obj-$(CONFIG_MMC_OCTEON) += octeon_mmc.o
  97. obj-$(CONFIG_MMC_MTK) += mtk-sd.o
  98. obj-$(CONFIG_MMC_OMAP) += omap.o
  99. obj-$(CONFIG_MMC_OMAP_HS) += omap_hsmmc.o
  100. --- /dev/null
  101. +++ b/drivers/mmc/host/octeon_mmc.c
  102. @@ -0,0 +1,1518 @@
  103. +/*
  104. + * Driver for MMC and SSD cards for Cavium OCTEON SOCs.
  105. + *
  106. + * This file is subject to the terms and conditions of the GNU General Public
  107. + * License. See the file "COPYING" in the main directory of this archive
  108. + * for more details.
  109. + *
  110. + * Copyright (C) 2012-2014 Cavium Inc.
  111. + */
  112. +
  113. +#include <linux/platform_device.h>
  114. +#include <linux/of_platform.h>
  115. +#include <linux/scatterlist.h>
  116. +#include <linux/interrupt.h>
  117. +#include <linux/of_gpio.h>
  118. +#include <linux/blkdev.h>
  119. +#include <linux/device.h>
  120. +#include <linux/module.h>
  121. +#include <linux/delay.h>
  122. +#include <linux/init.h>
  123. +#include <linux/clk.h>
  124. +#include <linux/err.h>
  125. +#include <linux/io.h>
  126. +#include <linux/of.h>
  127. +
  128. +#include <linux/mmc/card.h>
  129. +#include <linux/mmc/host.h>
  130. +#include <linux/mmc/mmc.h>
  131. +#include <linux/mmc/sd.h>
  132. +#include <net/irda/parameters.h>
  133. +
  134. +#include <asm/byteorder.h>
  135. +#include <asm/octeon/octeon.h>
  136. +#include <asm/octeon/cvmx-mio-defs.h>
  137. +
  138. +#define DRV_NAME "octeon_mmc"
  139. +
  140. +#define OCTEON_MAX_MMC 4
  141. +
  142. +#define OCT_MIO_NDF_DMA_CFG 0x00
  143. +#define OCT_MIO_EMM_DMA_ADR 0x08
  144. +
  145. +#define OCT_MIO_EMM_CFG 0x00
  146. +#define OCT_MIO_EMM_SWITCH 0x48
  147. +#define OCT_MIO_EMM_DMA 0x50
  148. +#define OCT_MIO_EMM_CMD 0x58
  149. +#define OCT_MIO_EMM_RSP_STS 0x60
  150. +#define OCT_MIO_EMM_RSP_LO 0x68
  151. +#define OCT_MIO_EMM_RSP_HI 0x70
  152. +#define OCT_MIO_EMM_INT 0x78
  153. +#define OCT_MIO_EMM_INT_EN 0x80
  154. +#define OCT_MIO_EMM_WDOG 0x88
  155. +#define OCT_MIO_EMM_SAMPLE 0x90
  156. +#define OCT_MIO_EMM_STS_MASK 0x98
  157. +#define OCT_MIO_EMM_RCA 0xa0
  158. +#define OCT_MIO_EMM_BUF_IDX 0xe0
  159. +#define OCT_MIO_EMM_BUF_DAT 0xe8
  160. +
  161. +#define CVMX_MIO_BOOT_CTL CVMX_ADD_IO_SEG(0x00011800000000D0ull)
  162. +
  163. +struct octeon_mmc_host {
  164. + u64 base;
  165. + u64 ndf_base;
  166. + u64 emm_cfg;
  167. + u64 n_minus_one; /* OCTEON II workaround location */
  168. + int last_slot;
  169. +
  170. + struct semaphore mmc_serializer;
  171. + struct mmc_request *current_req;
  172. + unsigned int linear_buf_size;
  173. + void *linear_buf;
  174. + struct sg_mapping_iter smi;
  175. + int sg_idx;
  176. + bool dma_active;
  177. +
  178. + struct platform_device *pdev;
  179. + int global_pwr_gpio;
  180. + bool global_pwr_gpio_low;
  181. + bool dma_err_pending;
  182. + bool need_bootbus_lock;
  183. + bool big_dma_addr;
  184. + bool need_irq_handler_lock;
  185. + spinlock_t irq_handler_lock;
  186. +
  187. + struct octeon_mmc_slot *slot[OCTEON_MAX_MMC];
  188. +};
  189. +
  190. +struct octeon_mmc_slot {
  191. + struct mmc_host *mmc; /* slot-level mmc_core object */
  192. + struct octeon_mmc_host *host; /* common hw for all 4 slots */
  193. +
  194. + unsigned int clock;
  195. + unsigned int sclock;
  196. +
  197. + u64 cached_switch;
  198. + u64 cached_rca;
  199. +
  200. + unsigned int cmd_cnt; /* sample delay */
  201. + unsigned int dat_cnt; /* sample delay */
  202. +
  203. + int bus_width;
  204. + int bus_id;
  205. + int ro_gpio;
  206. + int cd_gpio;
  207. + int pwr_gpio;
  208. + bool cd_gpio_low;
  209. + bool ro_gpio_low;
  210. + bool pwr_gpio_low;
  211. +};
  212. +
  213. +static int bb_size = 1 << 16;
  214. +module_param(bb_size, int, S_IRUGO);
  215. +MODULE_PARM_DESC(bb_size,
  216. + "Size of DMA linearizing buffer (max transfer size).");
  217. +
  218. +static int ddr = 2;
  219. +module_param(ddr, int, S_IRUGO);
  220. +MODULE_PARM_DESC(ddr,
  221. + "enable DoubleDataRate clocking: 0=no, 1=always, 2=at spi-max-frequency/2");
  222. +
  223. +#if 0
  224. +#define octeon_mmc_dbg trace_printk
  225. +#else
  226. +static inline void octeon_mmc_dbg(const char *s, ...) { }
  227. +#endif
  228. +
  229. +static void octeon_mmc_acquire_bus(struct octeon_mmc_host *host)
  230. +{
  231. + if (host->need_bootbus_lock) {
  232. + down(&octeon_bootbus_sem);
  233. + /* On cn70XX switch the mmc unit onto the bus. */
  234. + if (OCTEON_IS_MODEL(OCTEON_CN70XX))
  235. + cvmx_write_csr(CVMX_MIO_BOOT_CTL, 0);
  236. + } else {
  237. + down(&host->mmc_serializer);
  238. + }
  239. +}
  240. +
  241. +static void octeon_mmc_release_bus(struct octeon_mmc_host *host)
  242. +{
  243. + if (host->need_bootbus_lock)
  244. + up(&octeon_bootbus_sem);
  245. + else
  246. + up(&host->mmc_serializer);
  247. +}
  248. +
  249. +struct octeon_mmc_cr_type {
  250. + u8 ctype;
  251. + u8 rtype;
  252. +};
  253. +
  254. +/*
  255. + * The OCTEON MMC host hardware assumes that all commands have fixed
  256. + * command and response types. These are correct if MMC devices are
  257. + * being used. However, non-MMC devices like SD use command and
  258. + * response types that are unexpected by the host hardware.
  259. + *
  260. + * The command and response types can be overridden by supplying an
  261. + * XOR value that is applied to the type. We calculate the XOR value
  262. + * from the values in this table and the flags passed from the MMC
  263. + * core.
  264. + */
  265. +static struct octeon_mmc_cr_type octeon_mmc_cr_types[] = {
  266. + {0, 0}, /* CMD0 */
  267. + {0, 3}, /* CMD1 */
  268. + {0, 2}, /* CMD2 */
  269. + {0, 1}, /* CMD3 */
  270. + {0, 0}, /* CMD4 */
  271. + {0, 1}, /* CMD5 */
  272. + {0, 1}, /* CMD6 */
  273. + {0, 1}, /* CMD7 */
  274. + {1, 1}, /* CMD8 */
  275. + {0, 2}, /* CMD9 */
  276. + {0, 2}, /* CMD10 */
  277. + {1, 1}, /* CMD11 */
  278. + {0, 1}, /* CMD12 */
  279. + {0, 1}, /* CMD13 */
  280. + {1, 1}, /* CMD14 */
  281. + {0, 0}, /* CMD15 */
  282. + {0, 1}, /* CMD16 */
  283. + {1, 1}, /* CMD17 */
  284. + {1, 1}, /* CMD18 */
  285. + {3, 1}, /* CMD19 */
  286. + {2, 1}, /* CMD20 */
  287. + {0, 0}, /* CMD21 */
  288. + {0, 0}, /* CMD22 */
  289. + {0, 1}, /* CMD23 */
  290. + {2, 1}, /* CMD24 */
  291. + {2, 1}, /* CMD25 */
  292. + {2, 1}, /* CMD26 */
  293. + {2, 1}, /* CMD27 */
  294. + {0, 1}, /* CMD28 */
  295. + {0, 1}, /* CMD29 */
  296. + {1, 1}, /* CMD30 */
  297. + {1, 1}, /* CMD31 */
  298. + {0, 0}, /* CMD32 */
  299. + {0, 0}, /* CMD33 */
  300. + {0, 0}, /* CMD34 */
  301. + {0, 1}, /* CMD35 */
  302. + {0, 1}, /* CMD36 */
  303. + {0, 0}, /* CMD37 */
  304. + {0, 1}, /* CMD38 */
  305. + {0, 4}, /* CMD39 */
  306. + {0, 5}, /* CMD40 */
  307. + {0, 0}, /* CMD41 */
  308. + {2, 1}, /* CMD42 */
  309. + {0, 0}, /* CMD43 */
  310. + {0, 0}, /* CMD44 */
  311. + {0, 0}, /* CMD45 */
  312. + {0, 0}, /* CMD46 */
  313. + {0, 0}, /* CMD47 */
  314. + {0, 0}, /* CMD48 */
  315. + {0, 0}, /* CMD49 */
  316. + {0, 0}, /* CMD50 */
  317. + {0, 0}, /* CMD51 */
  318. + {0, 0}, /* CMD52 */
  319. + {0, 0}, /* CMD53 */
  320. + {0, 0}, /* CMD54 */
  321. + {0, 1}, /* CMD55 */
  322. + {0xff, 0xff}, /* CMD56 */
  323. + {0, 0}, /* CMD57 */
  324. + {0, 0}, /* CMD58 */
  325. + {0, 0}, /* CMD59 */
  326. + {0, 0}, /* CMD60 */
  327. + {0, 0}, /* CMD61 */
  328. + {0, 0}, /* CMD62 */
  329. + {0, 0} /* CMD63 */
  330. +};
  331. +
  332. +struct octeon_mmc_cr_mods {
  333. + u8 ctype_xor;
  334. + u8 rtype_xor;
  335. +};
  336. +
  337. +/*
  338. + * The functions below are used for the EMMC-17978 workaround.
  339. + *
  340. + * Due to an imperfection in the design of the MMC bus hardware,
  341. + * the 2nd to last cache block of a DMA read must be locked into the L2 Cache.
  342. + * Otherwise, data corruption may occur.
  343. + */
  344. +
  345. +static inline void *phys_to_ptr(u64 address)
  346. +{
  347. + return (void *)(address | (1ull<<63)); /* XKPHYS */
  348. +}
  349. +
  350. +/**
  351. + * Lock a single line into L2. The line is zeroed before locking
  352. + * to make sure no dram accesses are made.
  353. + *
  354. + * @addr Physical address to lock
  355. + */
  356. +static void l2c_lock_line(u64 addr)
  357. +{
  358. + char *addr_ptr = phys_to_ptr(addr);
  359. +
  360. + asm volatile (
  361. + "cache 31, %[line]" /* Unlock the line */
  362. + :: [line] "m" (*addr_ptr));
  363. +}
  364. +
  365. +/**
  366. + * Locks a memory region in the L2 cache
  367. + *
  368. + * @start - start address to begin locking
  369. + * @len - length in bytes to lock
  370. + */
  371. +static void l2c_lock_mem_region(u64 start, u64 len)
  372. +{
  373. + u64 end;
  374. +
  375. + /* Round start/end to cache line boundaries */
  376. + end = ALIGN(start + len - 1, CVMX_CACHE_LINE_SIZE);
  377. + start = ALIGN(start, CVMX_CACHE_LINE_SIZE);
  378. +
  379. + while (start <= end) {
  380. + l2c_lock_line(start);
  381. + start += CVMX_CACHE_LINE_SIZE;
  382. + }
  383. + asm volatile("sync");
  384. +}
  385. +
  386. +/**
  387. + * Unlock a single line in the L2 cache.
  388. + *
  389. + * @addr Physical address to unlock
  390. + *
  391. + * Return Zero on success
  392. + */
  393. +static void l2c_unlock_line(u64 addr)
  394. +{
  395. + char *addr_ptr = phys_to_ptr(addr);
  396. + asm volatile (
  397. + "cache 23, %[line]" /* Unlock the line */
  398. + :: [line] "m" (*addr_ptr));
  399. +}
  400. +
  401. +/**
  402. + * Unlock a memory region in the L2 cache
  403. + *
  404. + * @start - start address to unlock
  405. + * @len - length to unlock in bytes
  406. + */
  407. +static void l2c_unlock_mem_region(u64 start, u64 len)
  408. +{
  409. + u64 end;
  410. +
  411. + /* Round start/end to cache line boundaries */
  412. + end = ALIGN(start + len - 1, CVMX_CACHE_LINE_SIZE);
  413. + start = ALIGN(start, CVMX_CACHE_LINE_SIZE);
  414. +
  415. + while (start <= end) {
  416. + l2c_unlock_line(start);
  417. + start += CVMX_CACHE_LINE_SIZE;
  418. + }
  419. +}
  420. +
  421. +static struct octeon_mmc_cr_mods octeon_mmc_get_cr_mods(struct mmc_command *cmd)
  422. +{
  423. + struct octeon_mmc_cr_type *cr;
  424. + u8 desired_ctype, hardware_ctype;
  425. + u8 desired_rtype, hardware_rtype;
  426. + struct octeon_mmc_cr_mods r;
  427. +
  428. + desired_ctype = desired_rtype = 0;
  429. +
  430. + cr = octeon_mmc_cr_types + (cmd->opcode & 0x3f);
  431. + hardware_ctype = cr->ctype;
  432. + hardware_rtype = cr->rtype;
  433. + if (cmd->opcode == 56) { /* CMD56 GEN_CMD */
  434. + hardware_ctype = (cmd->arg & 1) ? 1 : 2;
  435. + }
  436. +
  437. + switch (mmc_cmd_type(cmd)) {
  438. + case MMC_CMD_ADTC:
  439. + desired_ctype = (cmd->data->flags & MMC_DATA_WRITE) ? 2 : 1;
  440. + break;
  441. + case MMC_CMD_AC:
  442. + case MMC_CMD_BC:
  443. + case MMC_CMD_BCR:
  444. + desired_ctype = 0;
  445. + break;
  446. + }
  447. +
  448. + switch (mmc_resp_type(cmd)) {
  449. + case MMC_RSP_NONE:
  450. + desired_rtype = 0;
  451. + break;
  452. + case MMC_RSP_R1:/* MMC_RSP_R5, MMC_RSP_R6, MMC_RSP_R7 */
  453. + case MMC_RSP_R1B:
  454. + desired_rtype = 1;
  455. + break;
  456. + case MMC_RSP_R2:
  457. + desired_rtype = 2;
  458. + break;
  459. + case MMC_RSP_R3: /* MMC_RSP_R4 */
  460. + desired_rtype = 3;
  461. + break;
  462. + }
  463. + r.ctype_xor = desired_ctype ^ hardware_ctype;
  464. + r.rtype_xor = desired_rtype ^ hardware_rtype;
  465. + return r;
  466. +}
  467. +
  468. +static bool octeon_mmc_switch_val_changed(struct octeon_mmc_slot *slot,
  469. + u64 new_val)
  470. +{
  471. + /* Match BUS_ID, HS_TIMING, BUS_WIDTH, POWER_CLASS, CLK_HI, CLK_LO */
  472. + u64 m = 0x3001070fffffffffull;
  473. +
  474. + return (slot->cached_switch & m) != (new_val & m);
  475. +}
  476. +
  477. +static unsigned int octeon_mmc_timeout_to_wdog(struct octeon_mmc_slot *slot,
  478. + unsigned int ns)
  479. +{
  480. + u64 bt = (u64)slot->clock * (u64)ns;
  481. +
  482. + return (unsigned int)(bt / 1000000000);
  483. +}
  484. +
  485. +static irqreturn_t octeon_mmc_interrupt(int irq, void *dev_id)
  486. +{
  487. + struct octeon_mmc_host *host = dev_id;
  488. + union cvmx_mio_emm_int emm_int;
  489. + struct mmc_request *req;
  490. + bool host_done;
  491. + union cvmx_mio_emm_rsp_sts rsp_sts;
  492. + unsigned long flags = 0;
  493. +
  494. + if (host->need_irq_handler_lock)
  495. + spin_lock_irqsave(&host->irq_handler_lock, flags);
  496. + emm_int.u64 = cvmx_read_csr(host->base + OCT_MIO_EMM_INT);
  497. + req = host->current_req;
  498. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT, emm_int.u64);
  499. +
  500. + octeon_mmc_dbg("Got interrupt: EMM_INT = 0x%llx\n", emm_int.u64);
  501. +
  502. + if (!req)
  503. + goto out;
  504. +
  505. + rsp_sts.u64 = cvmx_read_csr(host->base + OCT_MIO_EMM_RSP_STS);
  506. + octeon_mmc_dbg("octeon_mmc_interrupt MIO_EMM_RSP_STS 0x%llx\n",
  507. + rsp_sts.u64);
  508. +
  509. + if (host->dma_err_pending) {
  510. + host->current_req = NULL;
  511. + host->dma_err_pending = false;
  512. + req->done(req);
  513. + host_done = true;
  514. + goto no_req_done;
  515. + }
  516. +
  517. + if (!host->dma_active && emm_int.s.buf_done && req->data) {
  518. + unsigned int type = (rsp_sts.u64 >> 7) & 3;
  519. +
  520. + if (type == 1) {
  521. + /* Read */
  522. + int dbuf = rsp_sts.s.dbuf;
  523. + struct sg_mapping_iter *smi = &host->smi;
  524. + unsigned int data_len =
  525. + req->data->blksz * req->data->blocks;
  526. + unsigned int bytes_xfered;
  527. + u64 dat = 0;
  528. + int shift = -1;
  529. +
  530. + /* Auto inc from offset zero */
  531. + cvmx_write_csr(host->base + OCT_MIO_EMM_BUF_IDX,
  532. + (u64)(0x10000 | (dbuf << 6)));
  533. +
  534. + for (bytes_xfered = 0; bytes_xfered < data_len;) {
  535. + if (smi->consumed >= smi->length) {
  536. + if (!sg_miter_next(smi))
  537. + break;
  538. + smi->consumed = 0;
  539. + }
  540. + if (shift < 0) {
  541. + dat = cvmx_read_csr(host->base +
  542. + OCT_MIO_EMM_BUF_DAT);
  543. + shift = 56;
  544. + }
  545. +
  546. + while (smi->consumed < smi->length &&
  547. + shift >= 0) {
  548. + ((u8 *)(smi->addr))[smi->consumed] =
  549. + (dat >> shift) & 0xff;
  550. + bytes_xfered++;
  551. + smi->consumed++;
  552. + shift -= 8;
  553. + }
  554. + }
  555. + sg_miter_stop(smi);
  556. + req->data->bytes_xfered = bytes_xfered;
  557. + req->data->error = 0;
  558. + } else if (type == 2) {
  559. + /* write */
  560. + req->data->bytes_xfered = req->data->blksz *
  561. + req->data->blocks;
  562. + req->data->error = 0;
  563. + }
  564. + }
  565. + host_done = emm_int.s.cmd_done || emm_int.s.dma_done ||
  566. + emm_int.s.cmd_err || emm_int.s.dma_err;
  567. + if (host_done && req->done) {
  568. + if (rsp_sts.s.rsp_bad_sts ||
  569. + rsp_sts.s.rsp_crc_err ||
  570. + rsp_sts.s.rsp_timeout ||
  571. + rsp_sts.s.blk_crc_err ||
  572. + rsp_sts.s.blk_timeout ||
  573. + rsp_sts.s.dbuf_err) {
  574. + req->cmd->error = -EILSEQ;
  575. + } else {
  576. + req->cmd->error = 0;
  577. + }
  578. +
  579. + if (host->dma_active && req->data) {
  580. + req->data->error = 0;
  581. + req->data->bytes_xfered = req->data->blocks *
  582. + req->data->blksz;
  583. + if (!(req->data->flags & MMC_DATA_WRITE) &&
  584. + req->data->sg_len > 1) {
  585. + size_t r = sg_copy_from_buffer(req->data->sg,
  586. + req->data->sg_len, host->linear_buf,
  587. + req->data->bytes_xfered);
  588. + WARN_ON(r != req->data->bytes_xfered);
  589. + }
  590. + }
  591. + if (rsp_sts.s.rsp_val) {
  592. + u64 rsp_hi;
  593. + u64 rsp_lo = cvmx_read_csr(
  594. + host->base + OCT_MIO_EMM_RSP_LO);
  595. +
  596. + switch (rsp_sts.s.rsp_type) {
  597. + case 1:
  598. + case 3:
  599. + req->cmd->resp[0] = (rsp_lo >> 8) & 0xffffffff;
  600. + req->cmd->resp[1] = 0;
  601. + req->cmd->resp[2] = 0;
  602. + req->cmd->resp[3] = 0;
  603. + break;
  604. + case 2:
  605. + req->cmd->resp[3] = rsp_lo & 0xffffffff;
  606. + req->cmd->resp[2] = (rsp_lo >> 32) & 0xffffffff;
  607. + rsp_hi = cvmx_read_csr(host->base +
  608. + OCT_MIO_EMM_RSP_HI);
  609. + req->cmd->resp[1] = rsp_hi & 0xffffffff;
  610. + req->cmd->resp[0] = (rsp_hi >> 32) & 0xffffffff;
  611. + break;
  612. + default:
  613. + octeon_mmc_dbg("octeon_mmc_interrupt unhandled rsp_val %d\n",
  614. + rsp_sts.s.rsp_type);
  615. + break;
  616. + }
  617. + octeon_mmc_dbg("octeon_mmc_interrupt resp %08x %08x %08x %08x\n",
  618. + req->cmd->resp[0], req->cmd->resp[1],
  619. + req->cmd->resp[2], req->cmd->resp[3]);
  620. + }
  621. + if (emm_int.s.dma_err && rsp_sts.s.dma_pend) {
  622. + /* Try to clean up failed DMA */
  623. + union cvmx_mio_emm_dma emm_dma;
  624. +
  625. + emm_dma.u64 =
  626. + cvmx_read_csr(host->base + OCT_MIO_EMM_DMA);
  627. + emm_dma.s.dma_val = 1;
  628. + emm_dma.s.dat_null = 1;
  629. + emm_dma.s.bus_id = rsp_sts.s.bus_id;
  630. + cvmx_write_csr(host->base + OCT_MIO_EMM_DMA,
  631. + emm_dma.u64);
  632. + host->dma_err_pending = true;
  633. + host_done = false;
  634. + goto no_req_done;
  635. + }
  636. +
  637. + host->current_req = NULL;
  638. + req->done(req);
  639. + }
  640. +no_req_done:
  641. + if (host->n_minus_one) {
  642. + l2c_unlock_mem_region(host->n_minus_one, 512);
  643. + host->n_minus_one = 0;
  644. + }
  645. + if (host_done)
  646. + octeon_mmc_release_bus(host);
  647. +out:
  648. + if (host->need_irq_handler_lock)
  649. + spin_unlock_irqrestore(&host->irq_handler_lock, flags);
  650. + return IRQ_RETVAL(emm_int.u64 != 0);
  651. +}
  652. +
  653. +static void octeon_mmc_switch_to(struct octeon_mmc_slot *slot)
  654. +{
  655. + struct octeon_mmc_host *host = slot->host;
  656. + struct octeon_mmc_slot *old_slot;
  657. + union cvmx_mio_emm_switch sw;
  658. + union cvmx_mio_emm_sample samp;
  659. +
  660. + if (slot->bus_id == host->last_slot)
  661. + goto out;
  662. +
  663. + if (host->last_slot >= 0) {
  664. + old_slot = host->slot[host->last_slot];
  665. + old_slot->cached_switch =
  666. + cvmx_read_csr(host->base + OCT_MIO_EMM_SWITCH);
  667. + old_slot->cached_rca =
  668. + cvmx_read_csr(host->base + OCT_MIO_EMM_RCA);
  669. + }
  670. + cvmx_write_csr(host->base + OCT_MIO_EMM_RCA, slot->cached_rca);
  671. + sw.u64 = slot->cached_switch;
  672. + sw.s.bus_id = 0;
  673. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, sw.u64);
  674. + sw.s.bus_id = slot->bus_id;
  675. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, sw.u64);
  676. +
  677. + samp.u64 = 0;
  678. + samp.s.cmd_cnt = slot->cmd_cnt;
  679. + samp.s.dat_cnt = slot->dat_cnt;
  680. + cvmx_write_csr(host->base + OCT_MIO_EMM_SAMPLE, samp.u64);
  681. +out:
  682. + host->last_slot = slot->bus_id;
  683. +}
  684. +
  685. +static void octeon_mmc_dma_request(struct mmc_host *mmc,
  686. + struct mmc_request *mrq)
  687. +{
  688. + struct octeon_mmc_slot *slot;
  689. + struct octeon_mmc_host *host;
  690. + struct mmc_command *cmd;
  691. + struct mmc_data *data;
  692. + union cvmx_mio_emm_int emm_int;
  693. + union cvmx_mio_emm_dma emm_dma;
  694. + union cvmx_mio_ndf_dma_cfg dma_cfg;
  695. +
  696. + cmd = mrq->cmd;
  697. + if (mrq->data == NULL || mrq->data->sg == NULL || !mrq->data->sg_len ||
  698. + mrq->stop == NULL || mrq->stop->opcode != MMC_STOP_TRANSMISSION) {
  699. + dev_err(&mmc->card->dev,
  700. + "Error: octeon_mmc_dma_request no data\n");
  701. + cmd->error = -EINVAL;
  702. + if (mrq->done)
  703. + mrq->done(mrq);
  704. + return;
  705. + }
  706. +
  707. + slot = mmc_priv(mmc);
  708. + host = slot->host;
  709. +
  710. + /* Only a single user of the bootbus at a time. */
  711. + octeon_mmc_acquire_bus(host);
  712. +
  713. + octeon_mmc_switch_to(slot);
  714. +
  715. + data = mrq->data;
  716. +
  717. + if (data->timeout_ns) {
  718. + cvmx_write_csr(host->base + OCT_MIO_EMM_WDOG,
  719. + octeon_mmc_timeout_to_wdog(slot, data->timeout_ns));
  720. + octeon_mmc_dbg("OCT_MIO_EMM_WDOG %llu\n",
  721. + cvmx_read_csr(host->base + OCT_MIO_EMM_WDOG));
  722. + }
  723. +
  724. + WARN_ON(host->current_req);
  725. + host->current_req = mrq;
  726. +
  727. + host->sg_idx = 0;
  728. +
  729. + WARN_ON(data->blksz * data->blocks > host->linear_buf_size);
  730. +
  731. + if ((data->flags & MMC_DATA_WRITE) && data->sg_len > 1) {
  732. + size_t r = sg_copy_to_buffer(data->sg, data->sg_len,
  733. + host->linear_buf, data->blksz * data->blocks);
  734. + WARN_ON(data->blksz * data->blocks != r);
  735. + }
  736. +
  737. + dma_cfg.u64 = 0;
  738. + dma_cfg.s.en = 1;
  739. + dma_cfg.s.rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  740. +#ifdef __LITTLE_ENDIAN
  741. + dma_cfg.s.endian = 1;
  742. +#endif
  743. + dma_cfg.s.size = ((data->blksz * data->blocks) / 8) - 1;
  744. + if (!host->big_dma_addr) {
  745. + if (data->sg_len > 1)
  746. + dma_cfg.s.adr = virt_to_phys(host->linear_buf);
  747. + else
  748. + dma_cfg.s.adr = sg_phys(data->sg);
  749. + }
  750. + cvmx_write_csr(host->ndf_base + OCT_MIO_NDF_DMA_CFG, dma_cfg.u64);
  751. + octeon_mmc_dbg("MIO_NDF_DMA_CFG: %016llx\n",
  752. + (unsigned long long)dma_cfg.u64);
  753. + if (host->big_dma_addr) {
  754. + u64 addr;
  755. +
  756. + if (data->sg_len > 1)
  757. + addr = virt_to_phys(host->linear_buf);
  758. + else
  759. + addr = sg_phys(data->sg);
  760. + cvmx_write_csr(host->ndf_base + OCT_MIO_EMM_DMA_ADR, addr);
  761. + octeon_mmc_dbg("MIO_EMM_DMA_ADR: %016llx\n",
  762. + (unsigned long long)addr);
  763. + }
  764. +
  765. + emm_dma.u64 = 0;
  766. + emm_dma.s.bus_id = slot->bus_id;
  767. + emm_dma.s.dma_val = 1;
  768. + emm_dma.s.sector = mmc_card_blockaddr(mmc->card) ? 1 : 0;
  769. + emm_dma.s.rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  770. + if (mmc_card_mmc(mmc->card) ||
  771. + (mmc_card_sd(mmc->card) &&
  772. + (mmc->card->scr.cmds & SD_SCR_CMD23_SUPPORT)))
  773. + emm_dma.s.multi = 1;
  774. + emm_dma.s.block_cnt = data->blocks;
  775. + emm_dma.s.card_addr = cmd->arg;
  776. +
  777. + emm_int.u64 = 0;
  778. + emm_int.s.dma_done = 1;
  779. + emm_int.s.cmd_err = 1;
  780. + emm_int.s.dma_err = 1;
  781. + /* Clear the bit. */
  782. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT, emm_int.u64);
  783. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT_EN, emm_int.u64);
  784. + host->dma_active = true;
  785. +
  786. + if ((OCTEON_IS_MODEL(OCTEON_CN6XXX) ||
  787. + OCTEON_IS_MODEL(OCTEON_CNF7XXX)) &&
  788. + cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK &&
  789. + (data->blksz * data->blocks) > 1024) {
  790. + host->n_minus_one = dma_cfg.s.adr +
  791. + (data->blksz * data->blocks) - 1024;
  792. + l2c_lock_mem_region(host->n_minus_one, 512);
  793. + }
  794. +
  795. + if (mmc->card && mmc_card_sd(mmc->card))
  796. + cvmx_write_csr(host->base + OCT_MIO_EMM_STS_MASK,
  797. + 0x00b00000ull);
  798. + else
  799. + cvmx_write_csr(host->base + OCT_MIO_EMM_STS_MASK,
  800. + 0xe4f90080ull);
  801. + cvmx_write_csr(host->base + OCT_MIO_EMM_DMA, emm_dma.u64);
  802. + octeon_mmc_dbg("MIO_EMM_DMA: %llx\n", emm_dma.u64);
  803. +}
  804. +
  805. +static void octeon_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  806. +{
  807. + struct octeon_mmc_slot *slot;
  808. + struct octeon_mmc_host *host;
  809. + struct mmc_command *cmd;
  810. + union cvmx_mio_emm_int emm_int;
  811. + union cvmx_mio_emm_cmd emm_cmd;
  812. + struct octeon_mmc_cr_mods mods;
  813. +
  814. + cmd = mrq->cmd;
  815. +
  816. + if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  817. + cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  818. + octeon_mmc_dma_request(mmc, mrq);
  819. + return;
  820. + }
  821. +
  822. + mods = octeon_mmc_get_cr_mods(cmd);
  823. +
  824. + slot = mmc_priv(mmc);
  825. + host = slot->host;
  826. +
  827. + /* Only a single user of the bootbus at a time. */
  828. + octeon_mmc_acquire_bus(host);
  829. +
  830. + octeon_mmc_switch_to(slot);
  831. +
  832. + WARN_ON(host->current_req);
  833. + host->current_req = mrq;
  834. +
  835. + emm_int.u64 = 0;
  836. + emm_int.s.cmd_done = 1;
  837. + emm_int.s.cmd_err = 1;
  838. + if (cmd->data) {
  839. + octeon_mmc_dbg("command has data\n");
  840. + if (cmd->data->flags & MMC_DATA_READ) {
  841. + sg_miter_start(&host->smi, mrq->data->sg,
  842. + mrq->data->sg_len,
  843. + SG_MITER_ATOMIC | SG_MITER_TO_SG);
  844. + } else {
  845. + struct sg_mapping_iter *smi = &host->smi;
  846. + unsigned int data_len =
  847. + mrq->data->blksz * mrq->data->blocks;
  848. + unsigned int bytes_xfered;
  849. + u64 dat = 0;
  850. + int shift = 56;
  851. + /*
  852. + * Copy data to the xmit buffer before
  853. + * issuing the command
  854. + */
  855. + sg_miter_start(smi, mrq->data->sg,
  856. + mrq->data->sg_len, SG_MITER_FROM_SG);
  857. + /* Auto inc from offset zero, dbuf zero */
  858. + cvmx_write_csr(host->base + OCT_MIO_EMM_BUF_IDX,
  859. + 0x10000ull);
  860. +
  861. + for (bytes_xfered = 0; bytes_xfered < data_len;) {
  862. + if (smi->consumed >= smi->length) {
  863. + if (!sg_miter_next(smi))
  864. + break;
  865. + smi->consumed = 0;
  866. + }
  867. +
  868. + while (smi->consumed < smi->length &&
  869. + shift >= 0) {
  870. +
  871. + dat |= (u64)(((u8 *)(smi->addr))
  872. + [smi->consumed]) << shift;
  873. + bytes_xfered++;
  874. + smi->consumed++;
  875. + shift -= 8;
  876. + }
  877. + if (shift < 0) {
  878. + cvmx_write_csr(host->base +
  879. + OCT_MIO_EMM_BUF_DAT, dat);
  880. + shift = 56;
  881. + dat = 0;
  882. + }
  883. + }
  884. + sg_miter_stop(smi);
  885. + }
  886. + if (cmd->data->timeout_ns) {
  887. + cvmx_write_csr(host->base + OCT_MIO_EMM_WDOG,
  888. + octeon_mmc_timeout_to_wdog(slot,
  889. + cmd->data->timeout_ns));
  890. + octeon_mmc_dbg("OCT_MIO_EMM_WDOG %llu\n",
  891. + cvmx_read_csr(host->base +
  892. + OCT_MIO_EMM_WDOG));
  893. + }
  894. + } else {
  895. + cvmx_write_csr(host->base + OCT_MIO_EMM_WDOG,
  896. + ((u64)slot->clock * 850ull) / 1000ull);
  897. + octeon_mmc_dbg("OCT_MIO_EMM_WDOG %llu\n",
  898. + cvmx_read_csr(host->base + OCT_MIO_EMM_WDOG));
  899. + }
  900. + /* Clear the bit. */
  901. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT, emm_int.u64);
  902. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT_EN, emm_int.u64);
  903. + host->dma_active = false;
  904. +
  905. + emm_cmd.u64 = 0;
  906. + emm_cmd.s.cmd_val = 1;
  907. + emm_cmd.s.ctype_xor = mods.ctype_xor;
  908. + emm_cmd.s.rtype_xor = mods.rtype_xor;
  909. + if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  910. + emm_cmd.s.offset = 64 -
  911. + ((cmd->data->blksz * cmd->data->blocks) / 8);
  912. + emm_cmd.s.bus_id = slot->bus_id;
  913. + emm_cmd.s.cmd_idx = cmd->opcode;
  914. + emm_cmd.s.arg = cmd->arg;
  915. + cvmx_write_csr(host->base + OCT_MIO_EMM_STS_MASK, 0);
  916. + cvmx_write_csr(host->base + OCT_MIO_EMM_CMD, emm_cmd.u64);
  917. + octeon_mmc_dbg("MIO_EMM_CMD: %llx\n", emm_cmd.u64);
  918. +}
  919. +
  920. +static void octeon_mmc_reset_bus(struct octeon_mmc_slot *slot, int preserve)
  921. +{
  922. + union cvmx_mio_emm_cfg emm_cfg;
  923. + union cvmx_mio_emm_switch emm_switch;
  924. + u64 wdog = 0;
  925. +
  926. + emm_cfg.u64 = cvmx_read_csr(slot->host->base + OCT_MIO_EMM_CFG);
  927. + if (preserve) {
  928. + emm_switch.u64 = cvmx_read_csr(slot->host->base +
  929. + OCT_MIO_EMM_SWITCH);
  930. + wdog = cvmx_read_csr(slot->host->base + OCT_MIO_EMM_WDOG);
  931. + }
  932. +
  933. + /* Restore switch settings */
  934. + if (preserve) {
  935. + emm_switch.s.switch_exe = 0;
  936. + emm_switch.s.switch_err0 = 0;
  937. + emm_switch.s.switch_err1 = 0;
  938. + emm_switch.s.switch_err2 = 0;
  939. + emm_switch.s.bus_id = 0;
  940. + cvmx_write_csr(slot->host->base + OCT_MIO_EMM_SWITCH,
  941. + emm_switch.u64);
  942. + emm_switch.s.bus_id = slot->bus_id;
  943. + cvmx_write_csr(slot->host->base + OCT_MIO_EMM_SWITCH,
  944. + emm_switch.u64);
  945. +
  946. + slot->cached_switch = emm_switch.u64;
  947. +
  948. + msleep(10);
  949. + cvmx_write_csr(slot->host->base + OCT_MIO_EMM_WDOG, wdog);
  950. + } else {
  951. + slot->cached_switch = 0;
  952. + }
  953. +}
  954. +
  955. +static void octeon_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  956. +{
  957. + struct octeon_mmc_slot *slot;
  958. + struct octeon_mmc_host *host;
  959. + int bus_width;
  960. + int clock;
  961. + bool ddr_clock;
  962. + int hs_timing;
  963. + int power_class = 10;
  964. + int clk_period;
  965. + int timeout = 2000;
  966. + union cvmx_mio_emm_switch emm_switch;
  967. + union cvmx_mio_emm_rsp_sts emm_sts;
  968. +
  969. + slot = mmc_priv(mmc);
  970. + host = slot->host;
  971. +
  972. + /* Only a single user of the bootbus at a time. */
  973. + octeon_mmc_acquire_bus(host);
  974. +
  975. + octeon_mmc_switch_to(slot);
  976. +
  977. + octeon_mmc_dbg("Calling set_ios: slot: clk = 0x%x, bus_width = %d\n",
  978. + slot->clock, slot->bus_width);
  979. + octeon_mmc_dbg("Calling set_ios: ios: clk = 0x%x, vdd = %u, bus_width = %u, power_mode = %u, timing = %u\n",
  980. + ios->clock, ios->vdd, ios->bus_width, ios->power_mode,
  981. + ios->timing);
  982. + octeon_mmc_dbg("Calling set_ios: mmc: caps = 0x%x, bus_width = %d\n",
  983. + mmc->caps, mmc->ios.bus_width);
  984. +
  985. + /*
  986. + * Reset the chip on each power off
  987. + */
  988. + if (ios->power_mode == MMC_POWER_OFF) {
  989. + octeon_mmc_reset_bus(slot, 1);
  990. + if (slot->pwr_gpio >= 0)
  991. + gpio_set_value_cansleep(slot->pwr_gpio,
  992. + slot->pwr_gpio_low);
  993. + } else {
  994. + if (slot->pwr_gpio >= 0)
  995. + gpio_set_value_cansleep(slot->pwr_gpio,
  996. + !slot->pwr_gpio_low);
  997. + }
  998. +
  999. + switch (ios->bus_width) {
  1000. + case MMC_BUS_WIDTH_8:
  1001. + bus_width = 2;
  1002. + break;
  1003. + case MMC_BUS_WIDTH_4:
  1004. + bus_width = 1;
  1005. + break;
  1006. + case MMC_BUS_WIDTH_1:
  1007. + bus_width = 0;
  1008. + break;
  1009. + default:
  1010. + octeon_mmc_dbg("unknown bus width %d\n", ios->bus_width);
  1011. + bus_width = 0;
  1012. + break;
  1013. + }
  1014. +
  1015. + hs_timing = (ios->timing == MMC_TIMING_MMC_HS);
  1016. + ddr_clock = (bus_width && ios->timing >= MMC_TIMING_UHS_DDR50);
  1017. +
  1018. + if (ddr_clock)
  1019. + bus_width |= 4;
  1020. +
  1021. + if (ios->clock) {
  1022. + slot->clock = ios->clock;
  1023. + slot->bus_width = bus_width;
  1024. +
  1025. + clock = slot->clock;
  1026. +
  1027. + if (clock > 52000000)
  1028. + clock = 52000000;
  1029. +
  1030. + clk_period = (octeon_get_io_clock_rate() + clock - 1) /
  1031. + (2 * clock);
  1032. +
  1033. + /* until clock-renengotiate-on-CRC is in */
  1034. + if (ddr_clock && ddr > 1)
  1035. + clk_period *= 2;
  1036. +
  1037. + emm_switch.u64 = 0;
  1038. + emm_switch.s.hs_timing = hs_timing;
  1039. + emm_switch.s.bus_width = bus_width;
  1040. + emm_switch.s.power_class = power_class;
  1041. + emm_switch.s.clk_hi = clk_period;
  1042. + emm_switch.s.clk_lo = clk_period;
  1043. +
  1044. + if (!octeon_mmc_switch_val_changed(slot, emm_switch.u64)) {
  1045. + octeon_mmc_dbg("No change from 0x%llx mio_emm_switch, returning.\n",
  1046. + emm_switch.u64);
  1047. + goto out;
  1048. + }
  1049. +
  1050. + octeon_mmc_dbg("Writing 0x%llx to mio_emm_wdog\n",
  1051. + ((u64)clock * 850ull) / 1000ull);
  1052. + cvmx_write_csr(host->base + OCT_MIO_EMM_WDOG,
  1053. + ((u64)clock * 850ull) / 1000ull);
  1054. + octeon_mmc_dbg("Writing 0x%llx to mio_emm_switch\n",
  1055. + emm_switch.u64);
  1056. +
  1057. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, emm_switch.u64);
  1058. + emm_switch.s.bus_id = slot->bus_id;
  1059. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, emm_switch.u64);
  1060. + slot->cached_switch = emm_switch.u64;
  1061. +
  1062. + do {
  1063. + emm_sts.u64 =
  1064. + cvmx_read_csr(host->base + OCT_MIO_EMM_RSP_STS);
  1065. + if (!emm_sts.s.switch_val)
  1066. + break;
  1067. + udelay(100);
  1068. + } while (timeout-- > 0);
  1069. +
  1070. + if (timeout <= 0) {
  1071. + octeon_mmc_dbg("switch command timed out, status=0x%llx\n",
  1072. + emm_sts.u64);
  1073. + goto out;
  1074. + }
  1075. + }
  1076. +out:
  1077. + octeon_mmc_release_bus(host);
  1078. +}
  1079. +
  1080. +static int octeon_mmc_get_ro(struct mmc_host *mmc)
  1081. +{
  1082. + struct octeon_mmc_slot *slot = mmc_priv(mmc);
  1083. +
  1084. + if (slot->ro_gpio >= 0) {
  1085. + int pin = gpio_get_value_cansleep(slot->ro_gpio);
  1086. +
  1087. + if (pin < 0)
  1088. + return pin;
  1089. + if (slot->ro_gpio_low)
  1090. + pin = !pin;
  1091. + return pin;
  1092. + } else {
  1093. + return -ENOSYS;
  1094. + }
  1095. +}
  1096. +
  1097. +static int octeon_mmc_get_cd(struct mmc_host *mmc)
  1098. +{
  1099. + struct octeon_mmc_slot *slot = mmc_priv(mmc);
  1100. +
  1101. + if (slot->cd_gpio >= 0) {
  1102. + int pin = gpio_get_value_cansleep(slot->cd_gpio);
  1103. +
  1104. + if (pin < 0)
  1105. + return pin;
  1106. + if (slot->cd_gpio_low)
  1107. + pin = !pin;
  1108. + return pin;
  1109. + } else {
  1110. + return -ENOSYS;
  1111. + }
  1112. +}
  1113. +
  1114. +static const struct mmc_host_ops octeon_mmc_ops = {
  1115. + .request = octeon_mmc_request,
  1116. + .set_ios = octeon_mmc_set_ios,
  1117. + .get_ro = octeon_mmc_get_ro,
  1118. + .get_cd = octeon_mmc_get_cd,
  1119. +};
  1120. +
  1121. +static void octeon_mmc_set_clock(struct octeon_mmc_slot *slot,
  1122. + unsigned int clock)
  1123. +{
  1124. + struct mmc_host *mmc = slot->mmc;
  1125. +
  1126. + clock = min(clock, mmc->f_max);
  1127. + clock = max(clock, mmc->f_min);
  1128. + slot->clock = clock;
  1129. +}
  1130. +
  1131. +static int octeon_mmc_initlowlevel(struct octeon_mmc_slot *slot,
  1132. + int bus_width)
  1133. +{
  1134. + union cvmx_mio_emm_switch emm_switch;
  1135. + struct octeon_mmc_host *host = slot->host;
  1136. +
  1137. + host->emm_cfg |= 1ull << slot->bus_id;
  1138. + cvmx_write_csr(slot->host->base + OCT_MIO_EMM_CFG, host->emm_cfg);
  1139. + octeon_mmc_set_clock(slot, 400000);
  1140. +
  1141. + /* Program initial clock speed and power */
  1142. + emm_switch.u64 = 0;
  1143. + emm_switch.s.power_class = 10;
  1144. + emm_switch.s.clk_hi = (slot->sclock / slot->clock) / 2;
  1145. + emm_switch.s.clk_lo = (slot->sclock / slot->clock) / 2;
  1146. +
  1147. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, emm_switch.u64);
  1148. + emm_switch.s.bus_id = slot->bus_id;
  1149. + cvmx_write_csr(host->base + OCT_MIO_EMM_SWITCH, emm_switch.u64);
  1150. + slot->cached_switch = emm_switch.u64;
  1151. +
  1152. + cvmx_write_csr(host->base + OCT_MIO_EMM_WDOG,
  1153. + ((u64)slot->clock * 850ull) / 1000ull);
  1154. + cvmx_write_csr(host->base + OCT_MIO_EMM_STS_MASK, 0xe4f90080ull);
  1155. + cvmx_write_csr(host->base + OCT_MIO_EMM_RCA, 1);
  1156. + return 0;
  1157. +}
  1158. +
  1159. +static int __init octeon_init_slot(struct octeon_mmc_host *host, int id,
  1160. + int bus_width, int max_freq,
  1161. + int ro_gpio, int cd_gpio, int pwr_gpio,
  1162. + bool ro_low, bool cd_low, bool power_low,
  1163. + u32 cmd_skew, u32 dat_skew)
  1164. +{
  1165. + struct mmc_host *mmc;
  1166. + struct octeon_mmc_slot *slot;
  1167. + u64 clock_period;
  1168. + int ret;
  1169. +
  1170. + /*
  1171. + * Allocate MMC structue
  1172. + */
  1173. + mmc = mmc_alloc_host(sizeof(struct octeon_mmc_slot), &host->pdev->dev);
  1174. + if (!mmc) {
  1175. + dev_err(&host->pdev->dev, "alloc host failed\n");
  1176. + return -ENOMEM;
  1177. + }
  1178. +
  1179. + slot = mmc_priv(mmc);
  1180. + slot->mmc = mmc;
  1181. + slot->host = host;
  1182. + slot->ro_gpio = ro_gpio;
  1183. + slot->cd_gpio = cd_gpio;
  1184. + slot->pwr_gpio = pwr_gpio;
  1185. + slot->ro_gpio_low = ro_low;
  1186. + slot->cd_gpio_low = cd_low;
  1187. + slot->pwr_gpio_low = power_low;
  1188. +
  1189. + if (slot->ro_gpio >= 0) {
  1190. + ret = gpio_request(slot->ro_gpio, "mmc_ro");
  1191. + if (ret) {
  1192. + dev_err(&host->pdev->dev,
  1193. + "Could not request mmc_ro GPIO %d\n",
  1194. + slot->ro_gpio);
  1195. + return ret;
  1196. + }
  1197. + gpio_direction_input(slot->ro_gpio);
  1198. + }
  1199. + if (slot->cd_gpio >= 0) {
  1200. + ret = gpio_request(slot->cd_gpio, "mmc_card_detect");
  1201. + if (ret) {
  1202. + if (slot->ro_gpio >= 0)
  1203. + gpio_free(slot->ro_gpio);
  1204. + dev_err(&host->pdev->dev, "Could not request mmc_card_detect GPIO %d\n",
  1205. + slot->cd_gpio);
  1206. + return ret;
  1207. + }
  1208. + gpio_direction_input(slot->cd_gpio);
  1209. + }
  1210. + if (slot->pwr_gpio >= 0) {
  1211. + ret = gpio_request(slot->pwr_gpio, "mmc_power");
  1212. + if (ret) {
  1213. + dev_err(&host->pdev->dev,
  1214. + "Could not request mmc_power GPIO %d\n",
  1215. + slot->pwr_gpio);
  1216. + if (slot->ro_gpio >= 0)
  1217. + gpio_free(slot->ro_gpio);
  1218. + if (slot->cd_gpio)
  1219. + gpio_free(slot->cd_gpio);
  1220. + return ret;
  1221. + }
  1222. + octeon_mmc_dbg("%s: Shutting off power to slot %d via gpio %d\n",
  1223. + DRV_NAME, slot->bus_id, slot->pwr_gpio);
  1224. + gpio_direction_output(slot->pwr_gpio,
  1225. + slot->pwr_gpio_low);
  1226. + }
  1227. + /*
  1228. + * Set up host parameters.
  1229. + */
  1230. + mmc->ops = &octeon_mmc_ops;
  1231. + mmc->f_min = 400000;
  1232. + mmc->f_max = max_freq;
  1233. + mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  1234. + MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA |
  1235. + MMC_CAP_ERASE;
  1236. + mmc->ocr_avail = MMC_VDD_27_28 | MMC_VDD_28_29 | MMC_VDD_29_30 |
  1237. + MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33 |
  1238. + MMC_VDD_33_34 | MMC_VDD_34_35 | MMC_VDD_35_36;
  1239. +
  1240. + /* post-sdk23 caps */
  1241. + mmc->caps |=
  1242. + ((mmc->f_max >= 12000000) * MMC_CAP_UHS_SDR12) |
  1243. + ((mmc->f_max >= 25000000) * MMC_CAP_UHS_SDR25) |
  1244. + ((mmc->f_max >= 50000000) * MMC_CAP_UHS_SDR50) |
  1245. + MMC_CAP_CMD23;
  1246. +
  1247. + if (host->global_pwr_gpio >= 0)
  1248. + mmc->caps |= MMC_CAP_POWER_OFF_CARD;
  1249. +
  1250. + /* "1.8v" capability is actually 1.8-or-3.3v */
  1251. + if (ddr)
  1252. + mmc->caps |= MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR;
  1253. +
  1254. + mmc->max_segs = 64;
  1255. + mmc->max_seg_size = host->linear_buf_size;
  1256. + mmc->max_req_size = host->linear_buf_size;
  1257. + mmc->max_blk_size = 512;
  1258. + mmc->max_blk_count = mmc->max_req_size / 512;
  1259. +
  1260. + slot->clock = mmc->f_min;
  1261. + slot->sclock = octeon_get_io_clock_rate();
  1262. +
  1263. + clock_period = 1000000000000ull / slot->sclock; /* period in pS */
  1264. + slot->cmd_cnt = (cmd_skew + clock_period / 2) / clock_period;
  1265. + slot->dat_cnt = (dat_skew + clock_period / 2) / clock_period;
  1266. +
  1267. + slot->bus_width = bus_width;
  1268. + slot->bus_id = id;
  1269. + slot->cached_rca = 1;
  1270. +
  1271. + /* Only a single user of the bootbus at a time. */
  1272. + octeon_mmc_acquire_bus(host);
  1273. + host->slot[id] = slot;
  1274. +
  1275. + octeon_mmc_switch_to(slot);
  1276. + /* Initialize MMC Block. */
  1277. + octeon_mmc_initlowlevel(slot, bus_width);
  1278. +
  1279. + octeon_mmc_release_bus(host);
  1280. +
  1281. + ret = mmc_add_host(mmc);
  1282. + octeon_mmc_dbg("mmc_add_host returned %d\n", ret);
  1283. +
  1284. + return 0;
  1285. +}
  1286. +
  1287. +static int octeon_mmc_probe(struct platform_device *pdev)
  1288. +{
  1289. + union cvmx_mio_emm_cfg emm_cfg;
  1290. + struct octeon_mmc_host *host;
  1291. + struct resource *res;
  1292. + void __iomem *base;
  1293. + int mmc_irq[9];
  1294. + int i;
  1295. + int ret = 0;
  1296. + struct device_node *node = pdev->dev.of_node;
  1297. + bool cn78xx_style;
  1298. + u64 t;
  1299. + enum of_gpio_flags f;
  1300. +
  1301. + host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  1302. + if (!host)
  1303. + return -ENOMEM;
  1304. +
  1305. + spin_lock_init(&host->irq_handler_lock);
  1306. + sema_init(&host->mmc_serializer, 1);
  1307. +
  1308. + cn78xx_style = of_device_is_compatible(node, "cavium,octeon-7890-mmc");
  1309. + if (cn78xx_style) {
  1310. + host->need_bootbus_lock = false;
  1311. + host->big_dma_addr = true;
  1312. + host->need_irq_handler_lock = true;
  1313. + /*
  1314. + * First seven are the EMM_INT bits 0..6, then two for
  1315. + * the EMM_DMA_INT bits
  1316. + */
  1317. + for (i = 0; i < 9; i++) {
  1318. + mmc_irq[i] = platform_get_irq(pdev, i);
  1319. + if (mmc_irq[i] < 0)
  1320. + return mmc_irq[i];
  1321. + }
  1322. + } else {
  1323. + host->need_bootbus_lock = true;
  1324. + host->big_dma_addr = false;
  1325. + host->need_irq_handler_lock = false;
  1326. + /* First one is EMM second NDF_DMA */
  1327. + for (i = 0; i < 2; i++) {
  1328. + mmc_irq[i] = platform_get_irq(pdev, i);
  1329. + if (mmc_irq[i] < 0)
  1330. + return mmc_irq[i];
  1331. + }
  1332. + }
  1333. + host->last_slot = -1;
  1334. +
  1335. + if (bb_size < 512 || bb_size >= (1 << 24))
  1336. + bb_size = 1 << 16;
  1337. + host->linear_buf_size = bb_size;
  1338. + host->linear_buf = devm_kzalloc(&pdev->dev, host->linear_buf_size,
  1339. + GFP_KERNEL);
  1340. +
  1341. + if (!host->linear_buf) {
  1342. + dev_err(&pdev->dev, "devm_kzalloc failed\n");
  1343. + return -ENOMEM;
  1344. + }
  1345. +
  1346. + host->pdev = pdev;
  1347. +
  1348. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1349. + if (!res) {
  1350. + dev_err(&pdev->dev, "Platform resource[0] is missing\n");
  1351. + return -ENXIO;
  1352. + }
  1353. + base = devm_ioremap_resource(&pdev->dev, res);
  1354. + if (IS_ERR(base))
  1355. + return PTR_ERR(base);
  1356. + host->base = (u64)base;
  1357. +
  1358. + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  1359. + if (!res) {
  1360. + dev_err(&pdev->dev, "Platform resource[1] is missing\n");
  1361. + ret = -EINVAL;
  1362. + goto err;
  1363. + }
  1364. + base = devm_ioremap_resource(&pdev->dev, res);
  1365. + if (IS_ERR(base)) {
  1366. + ret = PTR_ERR(base);
  1367. + goto err;
  1368. + }
  1369. + host->ndf_base = (u64)base;
  1370. + /*
  1371. + * Clear out any pending interrupts that may be left over from
  1372. + * bootloader.
  1373. + */
  1374. + t = cvmx_read_csr(host->base + OCT_MIO_EMM_INT);
  1375. + cvmx_write_csr(host->base + OCT_MIO_EMM_INT, t);
  1376. + if (cn78xx_style) {
  1377. + /* Only CMD_DONE, DMA_DONE, CMD_ERR, DMA_ERR */
  1378. + for (i = 1; i <= 4; i++) {
  1379. + ret = devm_request_irq(&pdev->dev, mmc_irq[i],
  1380. + octeon_mmc_interrupt,
  1381. + 0, DRV_NAME, host);
  1382. + if (ret < 0) {
  1383. + dev_err(&pdev->dev, "Error: devm_request_irq %d\n",
  1384. + mmc_irq[i]);
  1385. + goto err;
  1386. + }
  1387. + }
  1388. + } else {
  1389. + ret = devm_request_irq(&pdev->dev, mmc_irq[0],
  1390. + octeon_mmc_interrupt, 0, DRV_NAME, host);
  1391. + if (ret < 0) {
  1392. + dev_err(&pdev->dev, "Error: devm_request_irq %d\n",
  1393. + mmc_irq[0]);
  1394. + goto err;
  1395. + }
  1396. + }
  1397. +
  1398. + ret = of_get_named_gpio_flags(node, "power-gpios", 0, &f);
  1399. + if (ret == -EPROBE_DEFER)
  1400. + goto err;
  1401. +
  1402. + host->global_pwr_gpio = ret;
  1403. + host->global_pwr_gpio_low =
  1404. + (host->global_pwr_gpio >= 0 && f == OF_GPIO_ACTIVE_LOW);
  1405. +
  1406. + if (host->global_pwr_gpio >= 0) {
  1407. + ret = gpio_request(host->global_pwr_gpio, "mmc global power");
  1408. + if (ret) {
  1409. + dev_err(&pdev->dev,
  1410. + "Could not request mmc global power gpio %d\n",
  1411. + host->global_pwr_gpio);
  1412. + goto err;
  1413. + }
  1414. + dev_dbg(&pdev->dev, "Global power on\n");
  1415. + gpio_direction_output(host->global_pwr_gpio,
  1416. + !host->global_pwr_gpio_low);
  1417. + }
  1418. +
  1419. + platform_set_drvdata(pdev, host);
  1420. +
  1421. + for_each_child_of_node(pdev->dev.of_node, node) {
  1422. +
  1423. + int r;
  1424. + u32 slot;
  1425. + int ro_gpio, cd_gpio, pwr_gpio;
  1426. + bool ro_low, cd_low, pwr_low;
  1427. + u32 bus_width, max_freq, cmd_skew, dat_skew;
  1428. +
  1429. + if (!of_device_is_compatible(node,
  1430. + "cavium,octeon-6130-mmc-slot")) {
  1431. + pr_warn("Sub node isn't slot: %s\n",
  1432. + of_node_full_name(node));
  1433. + continue;
  1434. + }
  1435. +
  1436. + if (of_property_read_u32(node, "reg", &slot) != 0) {
  1437. + pr_warn("Missing or invalid reg property on %s\n",
  1438. + of_node_full_name(node));
  1439. + continue;
  1440. + }
  1441. +
  1442. + r = of_property_read_u32(node, "cavium,bus-max-width",
  1443. + &bus_width);
  1444. + if (r) {
  1445. + bus_width = 8;
  1446. + pr_info("Bus width not found for slot %d, defaulting to %d\n",
  1447. + slot, bus_width);
  1448. + } else {
  1449. + switch (bus_width) {
  1450. + case 1:
  1451. + case 4:
  1452. + case 8:
  1453. + break;
  1454. + default:
  1455. + pr_warn("Invalid bus width property for slot %d\n",
  1456. + slot);
  1457. + continue;
  1458. + }
  1459. + }
  1460. +
  1461. + r = of_property_read_u32(node, "cavium,cmd-clk-skew",
  1462. + &cmd_skew);
  1463. + if (r)
  1464. + cmd_skew = 0;
  1465. +
  1466. + r = of_property_read_u32(node, "cavium,dat-clk-skew",
  1467. + &dat_skew);
  1468. + if (r)
  1469. + dat_skew = 0;
  1470. +
  1471. + r = of_property_read_u32(node, "spi-max-frequency", &max_freq);
  1472. + if (r) {
  1473. + max_freq = 52000000;
  1474. + pr_info("No spi-max-frequency for slot %d, defaulting to %d\n",
  1475. + slot, max_freq);
  1476. + }
  1477. +
  1478. + ro_gpio = of_get_named_gpio_flags(node, "wp-gpios", 0, &f);
  1479. + ro_low = (ro_gpio >= 0 && f == OF_GPIO_ACTIVE_LOW);
  1480. + cd_gpio = of_get_named_gpio_flags(node, "cd-gpios", 0, &f);
  1481. + cd_low = (cd_gpio >= 0 && f == OF_GPIO_ACTIVE_LOW);
  1482. + pwr_gpio = of_get_named_gpio_flags(node, "power-gpios", 0, &f);
  1483. + pwr_low = (pwr_gpio >= 0 && f == OF_GPIO_ACTIVE_LOW);
  1484. +
  1485. + ret = octeon_init_slot(host, slot, bus_width, max_freq,
  1486. + ro_gpio, cd_gpio, pwr_gpio,
  1487. + ro_low, cd_low, pwr_low,
  1488. + cmd_skew, dat_skew);
  1489. + octeon_mmc_dbg("init slot %d, ret = %d\n", slot, ret);
  1490. + if (ret)
  1491. + goto err;
  1492. + }
  1493. +
  1494. + return ret;
  1495. +
  1496. +err:
  1497. + dev_err(&pdev->dev, "Probe failed: %d\n", ret);
  1498. +
  1499. + /* Disable MMC controller */
  1500. + emm_cfg.s.bus_ena = 0;
  1501. + cvmx_write_csr(host->base + OCT_MIO_EMM_CFG, emm_cfg.u64);
  1502. +
  1503. + if (host->global_pwr_gpio >= 0) {
  1504. + dev_dbg(&pdev->dev, "Global power off\n");
  1505. + gpio_set_value_cansleep(host->global_pwr_gpio,
  1506. + host->global_pwr_gpio_low);
  1507. + gpio_free(host->global_pwr_gpio);
  1508. + }
  1509. +
  1510. + return ret;
  1511. +}
  1512. +
  1513. +static int octeon_mmc_remove(struct platform_device *pdev)
  1514. +{
  1515. + union cvmx_mio_ndf_dma_cfg ndf_dma_cfg;
  1516. + struct octeon_mmc_host *host = platform_get_drvdata(pdev);
  1517. + struct octeon_mmc_slot *slot;
  1518. +
  1519. + platform_set_drvdata(pdev, NULL);
  1520. +
  1521. + if (host) {
  1522. + int i;
  1523. +
  1524. + /* quench all users */
  1525. + for (i = 0; i < OCTEON_MAX_MMC; i++) {
  1526. + slot = host->slot[i];
  1527. + if (slot)
  1528. + mmc_remove_host(slot->mmc);
  1529. + }
  1530. +
  1531. + /* Reset bus_id */
  1532. + ndf_dma_cfg.u64 =
  1533. + cvmx_read_csr(host->ndf_base + OCT_MIO_NDF_DMA_CFG);
  1534. + ndf_dma_cfg.s.en = 0;
  1535. + cvmx_write_csr(host->ndf_base + OCT_MIO_NDF_DMA_CFG,
  1536. + ndf_dma_cfg.u64);
  1537. +
  1538. + for (i = 0; i < OCTEON_MAX_MMC; i++) {
  1539. + struct octeon_mmc_slot *slot;
  1540. +
  1541. + slot = host->slot[i];
  1542. + if (!slot)
  1543. + continue;
  1544. + /* Free the GPIOs */
  1545. + if (slot->ro_gpio >= 0)
  1546. + gpio_free(slot->ro_gpio);
  1547. + if (slot->cd_gpio >= 0)
  1548. + gpio_free(slot->cd_gpio);
  1549. + if (slot->pwr_gpio >= 0) {
  1550. + gpio_set_value_cansleep(slot->pwr_gpio,
  1551. + slot->pwr_gpio_low);
  1552. + gpio_free(slot->pwr_gpio);
  1553. + }
  1554. + }
  1555. +
  1556. + if (host->global_pwr_gpio >= 0) {
  1557. + dev_dbg(&pdev->dev, "Global power off\n");
  1558. + gpio_set_value_cansleep(host->global_pwr_gpio,
  1559. + host->global_pwr_gpio_low);
  1560. + gpio_free(host->global_pwr_gpio);
  1561. + }
  1562. +
  1563. + for (i = 0; i < OCTEON_MAX_MMC; i++) {
  1564. + slot = host->slot[i];
  1565. + if (slot)
  1566. + mmc_free_host(slot->mmc);
  1567. + }
  1568. +
  1569. + }
  1570. + return 0;
  1571. +}
  1572. +
  1573. +static struct of_device_id octeon_mmc_match[] = {
  1574. + {
  1575. + .compatible = "cavium,octeon-6130-mmc",
  1576. + },
  1577. + {
  1578. + .compatible = "cavium,octeon-7890-mmc",
  1579. + },
  1580. + {},
  1581. +};
  1582. +MODULE_DEVICE_TABLE(of, octeon_mmc_match);
  1583. +
  1584. +static struct platform_driver octeon_mmc_driver = {
  1585. + .probe = octeon_mmc_probe,
  1586. + .remove = octeon_mmc_remove,
  1587. + .driver = {
  1588. + .name = DRV_NAME,
  1589. + .owner = THIS_MODULE,
  1590. + .of_match_table = octeon_mmc_match,
  1591. + },
  1592. +};
  1593. +
  1594. +static int __init octeon_mmc_init(void)
  1595. +{
  1596. + int ret;
  1597. +
  1598. + octeon_mmc_dbg("calling octeon_mmc_init\n");
  1599. +
  1600. + ret = platform_driver_register(&octeon_mmc_driver);
  1601. + octeon_mmc_dbg("driver probe returned %d\n", ret);
  1602. +
  1603. + if (ret)
  1604. + pr_err("%s: Failed to register driver\n", DRV_NAME);
  1605. +
  1606. + return ret;
  1607. +}
  1608. +
  1609. +static void __exit octeon_mmc_cleanup(void)
  1610. +{
  1611. + /* Unregister MMC driver */
  1612. + platform_driver_unregister(&octeon_mmc_driver);
  1613. +}
  1614. +
  1615. +module_init(octeon_mmc_init);
  1616. +module_exit(octeon_mmc_cleanup);
  1617. +
  1618. +MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
  1619. +MODULE_DESCRIPTION("low-level driver for Cavium OCTEON MMC/SSD card");
  1620. +MODULE_LICENSE("GPL");