host.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. /*
  2. * linux/include/linux/mmc/host.h
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * Host driver specific definitions.
  9. */
  10. #ifndef LINUX_MMC_HOST_H
  11. #define LINUX_MMC_HOST_H
  12. #include <linux/leds.h>
  13. #include <linux/mutex.h>
  14. #include <linux/timer.h>
  15. #include <linux/sched.h>
  16. #include <linux/device.h>
  17. #include <linux/fault-inject.h>
  18. #include <linux/mmc/core.h>
  19. #include <linux/mmc/card.h>
  20. #include <linux/mmc/pm.h>
  21. struct mmc_ios {
  22. unsigned int clock; /* clock rate */
  23. unsigned short vdd;
  24. /* vdd stores the bit number of the selected voltage range from below. */
  25. unsigned char bus_mode; /* command output mode */
  26. #define MMC_BUSMODE_OPENDRAIN 1
  27. #define MMC_BUSMODE_PUSHPULL 2
  28. unsigned char chip_select; /* SPI chip select */
  29. #define MMC_CS_DONTCARE 0
  30. #define MMC_CS_HIGH 1
  31. #define MMC_CS_LOW 2
  32. unsigned char power_mode; /* power supply mode */
  33. #define MMC_POWER_OFF 0
  34. #define MMC_POWER_UP 1
  35. #define MMC_POWER_ON 2
  36. #define MMC_POWER_UNDEFINED 3
  37. unsigned char bus_width; /* data bus width */
  38. #define MMC_BUS_WIDTH_1 0
  39. #define MMC_BUS_WIDTH_4 2
  40. #define MMC_BUS_WIDTH_8 3
  41. unsigned char timing; /* timing specification used */
  42. #define MMC_TIMING_LEGACY 0
  43. #define MMC_TIMING_MMC_HS 1
  44. #define MMC_TIMING_SD_HS 2
  45. #define MMC_TIMING_UHS_SDR12 3
  46. #define MMC_TIMING_UHS_SDR25 4
  47. #define MMC_TIMING_UHS_SDR50 5
  48. #define MMC_TIMING_UHS_SDR104 6
  49. #define MMC_TIMING_UHS_DDR50 7
  50. #define MMC_TIMING_MMC_DDR52 8
  51. #define MMC_TIMING_MMC_HS200 9
  52. #define MMC_TIMING_MMC_HS400 10
  53. unsigned char signal_voltage; /* signalling voltage (1.8V or 3.3V) */
  54. #define MMC_SIGNAL_VOLTAGE_330 0
  55. #define MMC_SIGNAL_VOLTAGE_180 1
  56. #define MMC_SIGNAL_VOLTAGE_120 2
  57. unsigned char drv_type; /* driver type (A, B, C, D) */
  58. #define MMC_SET_DRIVER_TYPE_B 0
  59. #define MMC_SET_DRIVER_TYPE_A 1
  60. #define MMC_SET_DRIVER_TYPE_C 2
  61. #define MMC_SET_DRIVER_TYPE_D 3
  62. };
  63. struct mmc_host_ops {
  64. /*
  65. * It is optional for the host to implement pre_req and post_req in
  66. * order to support double buffering of requests (prepare one
  67. * request while another request is active).
  68. * pre_req() must always be followed by a post_req().
  69. * To undo a call made to pre_req(), call post_req() with
  70. * a nonzero err condition.
  71. */
  72. void (*post_req)(struct mmc_host *host, struct mmc_request *req,
  73. int err);
  74. void (*pre_req)(struct mmc_host *host, struct mmc_request *req,
  75. bool is_first_req);
  76. void (*request)(struct mmc_host *host, struct mmc_request *req);
  77. /*
  78. * Avoid calling these three functions too often or in a "fast path",
  79. * since underlaying controller might implement them in an expensive
  80. * and/or slow way.
  81. *
  82. * Also note that these functions might sleep, so don't call them
  83. * in the atomic contexts!
  84. *
  85. * Return values for the get_ro callback should be:
  86. * 0 for a read/write card
  87. * 1 for a read-only card
  88. * -ENOSYS when not supported (equal to NULL callback)
  89. * or a negative errno value when something bad happened
  90. *
  91. * Return values for the get_cd callback should be:
  92. * 0 for a absent card
  93. * 1 for a present card
  94. * -ENOSYS when not supported (equal to NULL callback)
  95. * or a negative errno value when something bad happened
  96. */
  97. void (*set_ios)(struct mmc_host *host, struct mmc_ios *ios);
  98. int (*get_ro)(struct mmc_host *host);
  99. int (*get_cd)(struct mmc_host *host);
  100. void (*enable_sdio_irq)(struct mmc_host *host, int enable);
  101. /* optional callback for HC quirks */
  102. void (*init_card)(struct mmc_host *host, struct mmc_card *card);
  103. int (*start_signal_voltage_switch)(struct mmc_host *host, struct mmc_ios *ios);
  104. /* Check if the card is pulling dat[0:3] low */
  105. int (*card_busy)(struct mmc_host *host);
  106. /* The tuning command opcode value is different for SD and eMMC cards */
  107. int (*execute_tuning)(struct mmc_host *host, u32 opcode);
  108. /* Prepare HS400 target operating frequency depending host driver */
  109. int (*prepare_hs400_tuning)(struct mmc_host *host, struct mmc_ios *ios);
  110. int (*select_drive_strength)(struct mmc_card *card,
  111. unsigned int max_dtr, int host_drv,
  112. int card_drv, int *drv_type);
  113. void (*hw_reset)(struct mmc_host *host);
  114. void (*card_event)(struct mmc_host *host);
  115. /*
  116. * Optional callback to support controllers with HW issues for multiple
  117. * I/O. Returns the number of supported blocks for the request.
  118. */
  119. int (*multi_io_quirk)(struct mmc_card *card,
  120. unsigned int direction, int blk_size);
  121. };
  122. struct mmc_card;
  123. struct device;
  124. struct mmc_async_req {
  125. /* active mmc request */
  126. struct mmc_request *mrq;
  127. /*
  128. * Check error status of completed mmc request.
  129. * Returns 0 if success otherwise non zero.
  130. */
  131. int (*err_check) (struct mmc_card *, struct mmc_async_req *);
  132. };
  133. /**
  134. * struct mmc_slot - MMC slot functions
  135. *
  136. * @cd_irq: MMC/SD-card slot hotplug detection IRQ or -EINVAL
  137. * @handler_priv: MMC/SD-card slot context
  138. *
  139. * Some MMC/SD host controllers implement slot-functions like card and
  140. * write-protect detection natively. However, a large number of controllers
  141. * leave these functions to the CPU. This struct provides a hook to attach
  142. * such slot-function drivers.
  143. */
  144. struct mmc_slot {
  145. int cd_irq;
  146. void *handler_priv;
  147. };
  148. /**
  149. * mmc_context_info - synchronization details for mmc context
  150. * @is_done_rcv wake up reason was done request
  151. * @is_new_req wake up reason was new request
  152. * @is_waiting_last_req mmc context waiting for single running request
  153. * @wait wait queue
  154. * @lock lock to protect data fields
  155. */
  156. struct mmc_context_info {
  157. bool is_done_rcv;
  158. bool is_new_req;
  159. bool is_waiting_last_req;
  160. wait_queue_head_t wait;
  161. spinlock_t lock;
  162. };
  163. struct regulator;
  164. struct mmc_pwrseq;
  165. struct mmc_supply {
  166. struct regulator *vmmc; /* Card power supply */
  167. struct regulator *vqmmc; /* Optional Vccq supply */
  168. };
  169. struct mmc_host {
  170. struct device *parent;
  171. struct device class_dev;
  172. int index;
  173. const struct mmc_host_ops *ops;
  174. struct mmc_pwrseq *pwrseq;
  175. unsigned int f_min;
  176. unsigned int f_max;
  177. unsigned int f_init;
  178. u32 ocr_avail;
  179. u32 ocr_avail_sdio; /* SDIO-specific OCR */
  180. u32 ocr_avail_sd; /* SD-specific OCR */
  181. u32 ocr_avail_mmc; /* MMC-specific OCR */
  182. struct notifier_block pm_notify;
  183. u32 max_current_330;
  184. u32 max_current_300;
  185. u32 max_current_180;
  186. #define MMC_VDD_165_195 0x00000080 /* VDD voltage 1.65 - 1.95 */
  187. #define MMC_VDD_20_21 0x00000100 /* VDD voltage 2.0 ~ 2.1 */
  188. #define MMC_VDD_21_22 0x00000200 /* VDD voltage 2.1 ~ 2.2 */
  189. #define MMC_VDD_22_23 0x00000400 /* VDD voltage 2.2 ~ 2.3 */
  190. #define MMC_VDD_23_24 0x00000800 /* VDD voltage 2.3 ~ 2.4 */
  191. #define MMC_VDD_24_25 0x00001000 /* VDD voltage 2.4 ~ 2.5 */
  192. #define MMC_VDD_25_26 0x00002000 /* VDD voltage 2.5 ~ 2.6 */
  193. #define MMC_VDD_26_27 0x00004000 /* VDD voltage 2.6 ~ 2.7 */
  194. #define MMC_VDD_27_28 0x00008000 /* VDD voltage 2.7 ~ 2.8 */
  195. #define MMC_VDD_28_29 0x00010000 /* VDD voltage 2.8 ~ 2.9 */
  196. #define MMC_VDD_29_30 0x00020000 /* VDD voltage 2.9 ~ 3.0 */
  197. #define MMC_VDD_30_31 0x00040000 /* VDD voltage 3.0 ~ 3.1 */
  198. #define MMC_VDD_31_32 0x00080000 /* VDD voltage 3.1 ~ 3.2 */
  199. #define MMC_VDD_32_33 0x00100000 /* VDD voltage 3.2 ~ 3.3 */
  200. #define MMC_VDD_33_34 0x00200000 /* VDD voltage 3.3 ~ 3.4 */
  201. #define MMC_VDD_34_35 0x00400000 /* VDD voltage 3.4 ~ 3.5 */
  202. #define MMC_VDD_35_36 0x00800000 /* VDD voltage 3.5 ~ 3.6 */
  203. u32 caps; /* Host capabilities */
  204. #define MMC_CAP_4_BIT_DATA (1 << 0) /* Can the host do 4 bit transfers */
  205. #define MMC_CAP_MMC_HIGHSPEED (1 << 1) /* Can do MMC high-speed timing */
  206. #define MMC_CAP_SD_HIGHSPEED (1 << 2) /* Can do SD high-speed timing */
  207. #define MMC_CAP_SDIO_IRQ (1 << 3) /* Can signal pending SDIO IRQs */
  208. #define MMC_CAP_SPI (1 << 4) /* Talks only SPI protocols */
  209. #define MMC_CAP_NEEDS_POLL (1 << 5) /* Needs polling for card-detection */
  210. #define MMC_CAP_8_BIT_DATA (1 << 6) /* Can the host do 8 bit transfers */
  211. #define MMC_CAP_AGGRESSIVE_PM (1 << 7) /* Suspend (e)MMC/SD at idle */
  212. #define MMC_CAP_NONREMOVABLE (1 << 8) /* Nonremovable e.g. eMMC */
  213. #define MMC_CAP_WAIT_WHILE_BUSY (1 << 9) /* Waits while card is busy */
  214. #define MMC_CAP_ERASE (1 << 10) /* Allow erase/trim commands */
  215. #define MMC_CAP_1_8V_DDR (1 << 11) /* can support */
  216. /* DDR mode at 1.8V */
  217. #define MMC_CAP_1_2V_DDR (1 << 12) /* can support */
  218. /* DDR mode at 1.2V */
  219. #define MMC_CAP_POWER_OFF_CARD (1 << 13) /* Can power off after boot */
  220. #define MMC_CAP_BUS_WIDTH_TEST (1 << 14) /* CMD14/CMD19 bus width ok */
  221. #define MMC_CAP_UHS_SDR12 (1 << 15) /* Host supports UHS SDR12 mode */
  222. #define MMC_CAP_UHS_SDR25 (1 << 16) /* Host supports UHS SDR25 mode */
  223. #define MMC_CAP_UHS_SDR50 (1 << 17) /* Host supports UHS SDR50 mode */
  224. #define MMC_CAP_UHS_SDR104 (1 << 18) /* Host supports UHS SDR104 mode */
  225. #define MMC_CAP_UHS_DDR50 (1 << 19) /* Host supports UHS DDR50 mode */
  226. #define MMC_CAP_DRIVER_TYPE_A (1 << 23) /* Host supports Driver Type A */
  227. #define MMC_CAP_DRIVER_TYPE_C (1 << 24) /* Host supports Driver Type C */
  228. #define MMC_CAP_DRIVER_TYPE_D (1 << 25) /* Host supports Driver Type D */
  229. #define MMC_CAP_CMD23 (1 << 30) /* CMD23 supported. */
  230. #define MMC_CAP_HW_RESET (1 << 31) /* Hardware reset */
  231. u32 caps2; /* More host capabilities */
  232. #define MMC_CAP2_BOOTPART_NOACC (1 << 0) /* Boot partition no access */
  233. #define MMC_CAP2_FULL_PWR_CYCLE (1 << 2) /* Can do full power cycle */
  234. #define MMC_CAP2_HS200_1_8V_SDR (1 << 5) /* can support */
  235. #define MMC_CAP2_HS200_1_2V_SDR (1 << 6) /* can support */
  236. #define MMC_CAP2_HS200 (MMC_CAP2_HS200_1_8V_SDR | \
  237. MMC_CAP2_HS200_1_2V_SDR)
  238. #define MMC_CAP2_HC_ERASE_SZ (1 << 9) /* High-capacity erase size */
  239. #define MMC_CAP2_CD_ACTIVE_HIGH (1 << 10) /* Card-detect signal active high */
  240. #define MMC_CAP2_RO_ACTIVE_HIGH (1 << 11) /* Write-protect signal active high */
  241. #define MMC_CAP2_PACKED_RD (1 << 12) /* Allow packed read */
  242. #define MMC_CAP2_PACKED_WR (1 << 13) /* Allow packed write */
  243. #define MMC_CAP2_PACKED_CMD (MMC_CAP2_PACKED_RD | \
  244. MMC_CAP2_PACKED_WR)
  245. #define MMC_CAP2_NO_PRESCAN_POWERUP (1 << 14) /* Don't power up before scan */
  246. #define MMC_CAP2_HS400_1_8V (1 << 15) /* Can support HS400 1.8V */
  247. #define MMC_CAP2_HS400_1_2V (1 << 16) /* Can support HS400 1.2V */
  248. #define MMC_CAP2_HS400 (MMC_CAP2_HS400_1_8V | \
  249. MMC_CAP2_HS400_1_2V)
  250. #define MMC_CAP2_HSX00_1_2V (MMC_CAP2_HS200_1_2V_SDR | MMC_CAP2_HS400_1_2V)
  251. #define MMC_CAP2_SDIO_IRQ_NOTHREAD (1 << 17)
  252. #define MMC_CAP2_NO_WRITE_PROTECT (1 << 18) /* No physical write protect pin, assume that card is always read-write */
  253. mmc_pm_flag_t pm_caps; /* supported pm features */
  254. /* host specific block data */
  255. unsigned int max_seg_size; /* see blk_queue_max_segment_size */
  256. unsigned short max_segs; /* see blk_queue_max_segments */
  257. unsigned short unused;
  258. unsigned int max_req_size; /* maximum number of bytes in one req */
  259. unsigned int max_blk_size; /* maximum size of one mmc block */
  260. unsigned int max_blk_count; /* maximum number of blocks in one req */
  261. unsigned int max_busy_timeout; /* max busy timeout in ms */
  262. /* private data */
  263. spinlock_t lock; /* lock for claim and bus ops */
  264. struct mmc_ios ios; /* current io bus settings */
  265. /* group bitfields together to minimize padding */
  266. unsigned int use_spi_crc:1;
  267. unsigned int claimed:1; /* host exclusively claimed */
  268. unsigned int bus_dead:1; /* bus has been released */
  269. #ifdef CONFIG_MMC_DEBUG
  270. unsigned int removed:1; /* host is being removed */
  271. #endif
  272. unsigned int can_retune:1; /* re-tuning can be used */
  273. unsigned int doing_retune:1; /* re-tuning in progress */
  274. unsigned int retune_now:1; /* do re-tuning at next req */
  275. int rescan_disable; /* disable card detection */
  276. int rescan_entered; /* used with nonremovable devices */
  277. int need_retune; /* re-tuning is needed */
  278. int hold_retune; /* hold off re-tuning */
  279. unsigned int retune_period; /* re-tuning period in secs */
  280. struct timer_list retune_timer; /* for periodic re-tuning */
  281. bool trigger_card_event; /* card_event necessary */
  282. struct mmc_card *card; /* device attached to this host */
  283. wait_queue_head_t wq;
  284. struct task_struct *claimer; /* task that has host claimed */
  285. int claim_cnt; /* "claim" nesting count */
  286. struct delayed_work detect;
  287. int detect_change; /* card detect flag */
  288. struct mmc_slot slot;
  289. const struct mmc_bus_ops *bus_ops; /* current bus driver */
  290. unsigned int bus_refs; /* reference counter */
  291. unsigned int sdio_irqs;
  292. struct task_struct *sdio_irq_thread;
  293. bool sdio_irq_pending;
  294. atomic_t sdio_irq_thread_abort;
  295. mmc_pm_flag_t pm_flags; /* requested pm features */
  296. struct led_trigger *led; /* activity led */
  297. #ifdef CONFIG_REGULATOR
  298. bool regulator_enabled; /* regulator state */
  299. #endif
  300. struct mmc_supply supply;
  301. struct dentry *debugfs_root;
  302. struct mmc_async_req *areq; /* active async req */
  303. struct mmc_context_info context_info; /* async synchronization info */
  304. #ifdef CONFIG_FAIL_MMC_REQUEST
  305. struct fault_attr fail_mmc_request;
  306. #endif
  307. unsigned int actual_clock; /* Actual HC clock rate */
  308. unsigned int slotno; /* used for sdio acpi binding */
  309. int dsr_req; /* DSR value is valid */
  310. u32 dsr; /* optional driver stage (DSR) value */
  311. unsigned long private[0] ____cacheline_aligned;
  312. };
  313. struct mmc_host *mmc_alloc_host(int extra, struct device *);
  314. int mmc_add_host(struct mmc_host *);
  315. void mmc_remove_host(struct mmc_host *);
  316. void mmc_free_host(struct mmc_host *);
  317. int mmc_of_parse(struct mmc_host *host);
  318. static inline void *mmc_priv(struct mmc_host *host)
  319. {
  320. return (void *)host->private;
  321. }
  322. #define mmc_host_is_spi(host) ((host)->caps & MMC_CAP_SPI)
  323. #define mmc_dev(x) ((x)->parent)
  324. #define mmc_classdev(x) (&(x)->class_dev)
  325. #define mmc_hostname(x) (dev_name(&(x)->class_dev))
  326. int mmc_power_save_host(struct mmc_host *host);
  327. int mmc_power_restore_host(struct mmc_host *host);
  328. void mmc_detect_change(struct mmc_host *, unsigned long delay);
  329. void mmc_request_done(struct mmc_host *, struct mmc_request *);
  330. static inline void mmc_signal_sdio_irq(struct mmc_host *host)
  331. {
  332. host->ops->enable_sdio_irq(host, 0);
  333. host->sdio_irq_pending = true;
  334. if (host->sdio_irq_thread)
  335. wake_up_process(host->sdio_irq_thread);
  336. }
  337. void sdio_run_irqs(struct mmc_host *host);
  338. #ifdef CONFIG_REGULATOR
  339. int mmc_regulator_get_ocrmask(struct regulator *supply);
  340. int mmc_regulator_set_ocr(struct mmc_host *mmc,
  341. struct regulator *supply,
  342. unsigned short vdd_bit);
  343. int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios);
  344. #else
  345. static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
  346. {
  347. return 0;
  348. }
  349. static inline int mmc_regulator_set_ocr(struct mmc_host *mmc,
  350. struct regulator *supply,
  351. unsigned short vdd_bit)
  352. {
  353. return 0;
  354. }
  355. static inline int mmc_regulator_set_vqmmc(struct mmc_host *mmc,
  356. struct mmc_ios *ios)
  357. {
  358. return -EINVAL;
  359. }
  360. #endif
  361. int mmc_regulator_get_supply(struct mmc_host *mmc);
  362. int mmc_pm_notify(struct notifier_block *notify_block, unsigned long, void *);
  363. static inline int mmc_card_is_removable(struct mmc_host *host)
  364. {
  365. return !(host->caps & MMC_CAP_NONREMOVABLE);
  366. }
  367. static inline int mmc_card_keep_power(struct mmc_host *host)
  368. {
  369. return host->pm_flags & MMC_PM_KEEP_POWER;
  370. }
  371. static inline int mmc_card_wake_sdio_irq(struct mmc_host *host)
  372. {
  373. return host->pm_flags & MMC_PM_WAKE_SDIO_IRQ;
  374. }
  375. static inline int mmc_host_cmd23(struct mmc_host *host)
  376. {
  377. return host->caps & MMC_CAP_CMD23;
  378. }
  379. static inline int mmc_boot_partition_access(struct mmc_host *host)
  380. {
  381. return !(host->caps2 & MMC_CAP2_BOOTPART_NOACC);
  382. }
  383. static inline int mmc_host_uhs(struct mmc_host *host)
  384. {
  385. return host->caps &
  386. (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  387. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  388. MMC_CAP_UHS_DDR50);
  389. }
  390. static inline int mmc_host_packed_wr(struct mmc_host *host)
  391. {
  392. return host->caps2 & MMC_CAP2_PACKED_WR;
  393. }
  394. static inline int mmc_card_hs(struct mmc_card *card)
  395. {
  396. return card->host->ios.timing == MMC_TIMING_SD_HS ||
  397. card->host->ios.timing == MMC_TIMING_MMC_HS;
  398. }
  399. static inline int mmc_card_uhs(struct mmc_card *card)
  400. {
  401. return card->host->ios.timing >= MMC_TIMING_UHS_SDR12 &&
  402. card->host->ios.timing <= MMC_TIMING_UHS_DDR50;
  403. }
  404. static inline bool mmc_card_hs200(struct mmc_card *card)
  405. {
  406. return card->host->ios.timing == MMC_TIMING_MMC_HS200;
  407. }
  408. static inline bool mmc_card_ddr52(struct mmc_card *card)
  409. {
  410. return card->host->ios.timing == MMC_TIMING_MMC_DDR52;
  411. }
  412. static inline bool mmc_card_hs400(struct mmc_card *card)
  413. {
  414. return card->host->ios.timing == MMC_TIMING_MMC_HS400;
  415. }
  416. void mmc_retune_timer_stop(struct mmc_host *host);
  417. static inline void mmc_retune_needed(struct mmc_host *host)
  418. {
  419. if (host->can_retune)
  420. host->need_retune = 1;
  421. }
  422. static inline void mmc_retune_recheck(struct mmc_host *host)
  423. {
  424. if (host->hold_retune <= 1)
  425. host->retune_now = 1;
  426. }
  427. #endif /* LINUX_MMC_HOST_H */