ufshcd.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  1. /*
  2. * Universal Flash Storage Host controller driver
  3. *
  4. * This code is based on drivers/scsi/ufs/ufshcd.h
  5. * Copyright (C) 2011-2013 Samsung India Software Operations
  6. *
  7. * Authors:
  8. * Santosh Yaraganavi <santosh.sy@samsung.com>
  9. * Vinayak Holikatti <h.vinayak@samsung.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. * See the COPYING file in the top-level directory or visit
  16. * <http://www.gnu.org/licenses/gpl-2.0.html>
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * This program is provided "AS IS" and "WITH ALL FAULTS" and
  24. * without warranty of any kind. You are solely responsible for
  25. * determining the appropriateness of using and distributing
  26. * the program and assume all risks associated with your exercise
  27. * of rights with respect to the program, including but not limited
  28. * to infringement of third party rights, the risks and costs of
  29. * program errors, damage to or loss of data, programs or equipment,
  30. * and unavailability or interruption of operations. Under no
  31. * circumstances will the contributor of this Program be liable for
  32. * any damages of any kind arising from your use or distribution of
  33. * this program.
  34. */
  35. #ifndef _UFSHCD_H
  36. #define _UFSHCD_H
  37. #include <linux/module.h>
  38. #include <linux/kernel.h>
  39. #include <linux/init.h>
  40. #include <linux/interrupt.h>
  41. #include <linux/io.h>
  42. #include <linux/delay.h>
  43. #include <linux/slab.h>
  44. #include <linux/spinlock.h>
  45. #include <linux/workqueue.h>
  46. #include <linux/errno.h>
  47. #include <linux/types.h>
  48. #include <linux/wait.h>
  49. #include <linux/bitops.h>
  50. #include <linux/pm_runtime.h>
  51. #include <linux/clk.h>
  52. #include <linux/completion.h>
  53. #include <linux/regulator/consumer.h>
  54. #include <asm/irq.h>
  55. #include <asm/byteorder.h>
  56. #include <scsi/scsi.h>
  57. #include <scsi/scsi_cmnd.h>
  58. #include <scsi/scsi_host.h>
  59. #include <scsi/scsi_tcq.h>
  60. #include <scsi/scsi_dbg.h>
  61. #include <scsi/scsi_eh.h>
  62. #include "ufs.h"
  63. #include "ufshci.h"
  64. #define UFSHCD "ufshcd"
  65. #define UFSHCD_DRIVER_VERSION "0.2"
  66. struct ufs_hba;
  67. enum dev_cmd_type {
  68. DEV_CMD_TYPE_NOP = 0x0,
  69. DEV_CMD_TYPE_QUERY = 0x1,
  70. };
  71. /**
  72. * struct uic_command - UIC command structure
  73. * @command: UIC command
  74. * @argument1: UIC command argument 1
  75. * @argument2: UIC command argument 2
  76. * @argument3: UIC command argument 3
  77. * @cmd_active: Indicate if UIC command is outstanding
  78. * @result: UIC command result
  79. * @done: UIC command completion
  80. */
  81. struct uic_command {
  82. u32 command;
  83. u32 argument1;
  84. u32 argument2;
  85. u32 argument3;
  86. int cmd_active;
  87. int result;
  88. struct completion done;
  89. };
  90. /* Used to differentiate the power management options */
  91. enum ufs_pm_op {
  92. UFS_RUNTIME_PM,
  93. UFS_SYSTEM_PM,
  94. UFS_SHUTDOWN_PM,
  95. };
  96. #define ufshcd_is_runtime_pm(op) ((op) == UFS_RUNTIME_PM)
  97. #define ufshcd_is_system_pm(op) ((op) == UFS_SYSTEM_PM)
  98. #define ufshcd_is_shutdown_pm(op) ((op) == UFS_SHUTDOWN_PM)
  99. /* Host <-> Device UniPro Link state */
  100. enum uic_link_state {
  101. UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */
  102. UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */
  103. UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */
  104. };
  105. #define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)
  106. #define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \
  107. UIC_LINK_ACTIVE_STATE)
  108. #define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \
  109. UIC_LINK_HIBERN8_STATE)
  110. #define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)
  111. #define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \
  112. UIC_LINK_ACTIVE_STATE)
  113. #define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \
  114. UIC_LINK_HIBERN8_STATE)
  115. /*
  116. * UFS Power management levels.
  117. * Each level is in increasing order of power savings.
  118. */
  119. enum ufs_pm_level {
  120. UFS_PM_LVL_0, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE */
  121. UFS_PM_LVL_1, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  122. UFS_PM_LVL_2, /* UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE */
  123. UFS_PM_LVL_3, /* UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  124. UFS_PM_LVL_4, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE */
  125. UFS_PM_LVL_5, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE */
  126. UFS_PM_LVL_MAX
  127. };
  128. struct ufs_pm_lvl_states {
  129. enum ufs_dev_pwr_mode dev_state;
  130. enum uic_link_state link_state;
  131. };
  132. /**
  133. * struct ufshcd_lrb - local reference block
  134. * @utr_descriptor_ptr: UTRD address of the command
  135. * @ucd_req_ptr: UCD address of the command
  136. * @ucd_rsp_ptr: Response UPIU address for this command
  137. * @ucd_prdt_ptr: PRDT address of the command
  138. * @cmd: pointer to SCSI command
  139. * @sense_buffer: pointer to sense buffer address of the SCSI command
  140. * @sense_bufflen: Length of the sense buffer
  141. * @scsi_status: SCSI status of the command
  142. * @command_type: SCSI, UFS, Query.
  143. * @task_tag: Task tag of the command
  144. * @lun: LUN of the command
  145. * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)
  146. */
  147. struct ufshcd_lrb {
  148. struct utp_transfer_req_desc *utr_descriptor_ptr;
  149. struct utp_upiu_req *ucd_req_ptr;
  150. struct utp_upiu_rsp *ucd_rsp_ptr;
  151. struct ufshcd_sg_entry *ucd_prdt_ptr;
  152. struct scsi_cmnd *cmd;
  153. u8 *sense_buffer;
  154. unsigned int sense_bufflen;
  155. int scsi_status;
  156. int command_type;
  157. int task_tag;
  158. u8 lun; /* UPIU LUN id field is only 8-bit wide */
  159. bool intr_cmd;
  160. };
  161. /**
  162. * struct ufs_query - holds relevent data structures for query request
  163. * @request: request upiu and function
  164. * @descriptor: buffer for sending/receiving descriptor
  165. * @response: response upiu and response
  166. */
  167. struct ufs_query {
  168. struct ufs_query_req request;
  169. u8 *descriptor;
  170. struct ufs_query_res response;
  171. };
  172. /**
  173. * struct ufs_dev_cmd - all assosiated fields with device management commands
  174. * @type: device management command type - Query, NOP OUT
  175. * @lock: lock to allow one command at a time
  176. * @complete: internal commands completion
  177. * @tag_wq: wait queue until free command slot is available
  178. */
  179. struct ufs_dev_cmd {
  180. enum dev_cmd_type type;
  181. struct mutex lock;
  182. struct completion *complete;
  183. wait_queue_head_t tag_wq;
  184. struct ufs_query query;
  185. };
  186. /**
  187. * struct ufs_clk_info - UFS clock related info
  188. * @list: list headed by hba->clk_list_head
  189. * @clk: clock node
  190. * @name: clock name
  191. * @max_freq: maximum frequency supported by the clock
  192. * @min_freq: min frequency that can be used for clock scaling
  193. * @curr_freq: indicates the current frequency that it is set to
  194. * @enabled: variable to check against multiple enable/disable
  195. */
  196. struct ufs_clk_info {
  197. struct list_head list;
  198. struct clk *clk;
  199. const char *name;
  200. u32 max_freq;
  201. u32 min_freq;
  202. u32 curr_freq;
  203. bool enabled;
  204. };
  205. enum ufs_notify_change_status {
  206. PRE_CHANGE,
  207. POST_CHANGE,
  208. };
  209. struct ufs_pa_layer_attr {
  210. u32 gear_rx;
  211. u32 gear_tx;
  212. u32 lane_rx;
  213. u32 lane_tx;
  214. u32 pwr_rx;
  215. u32 pwr_tx;
  216. u32 hs_rate;
  217. };
  218. struct ufs_pwr_mode_info {
  219. bool is_valid;
  220. struct ufs_pa_layer_attr info;
  221. };
  222. /**
  223. * struct ufs_hba_variant_ops - variant specific callbacks
  224. * @name: variant name
  225. * @init: called when the driver is initialized
  226. * @exit: called to cleanup everything done in init
  227. * @get_ufs_hci_version: called to get UFS HCI version
  228. * @clk_scale_notify: notifies that clks are scaled up/down
  229. * @setup_clocks: called before touching any of the controller registers
  230. * @setup_regulators: called before accessing the host controller
  231. * @hce_enable_notify: called before and after HCE enable bit is set to allow
  232. * variant specific Uni-Pro initialization.
  233. * @link_startup_notify: called before and after Link startup is carried out
  234. * to allow variant specific Uni-Pro initialization.
  235. * @pwr_change_notify: called before and after a power mode change
  236. * is carried out to allow vendor spesific capabilities
  237. * to be set.
  238. * @suspend: called during host controller PM callback
  239. * @resume: called during host controller PM callback
  240. * @dbg_register_dump: used to dump controller debug information
  241. */
  242. struct ufs_hba_variant_ops {
  243. const char *name;
  244. int (*init)(struct ufs_hba *);
  245. void (*exit)(struct ufs_hba *);
  246. u32 (*get_ufs_hci_version)(struct ufs_hba *);
  247. int (*clk_scale_notify)(struct ufs_hba *, bool,
  248. enum ufs_notify_change_status);
  249. int (*setup_clocks)(struct ufs_hba *, bool);
  250. int (*setup_regulators)(struct ufs_hba *, bool);
  251. int (*hce_enable_notify)(struct ufs_hba *,
  252. enum ufs_notify_change_status);
  253. int (*link_startup_notify)(struct ufs_hba *,
  254. enum ufs_notify_change_status);
  255. int (*pwr_change_notify)(struct ufs_hba *,
  256. enum ufs_notify_change_status status,
  257. struct ufs_pa_layer_attr *,
  258. struct ufs_pa_layer_attr *);
  259. int (*suspend)(struct ufs_hba *, enum ufs_pm_op);
  260. int (*resume)(struct ufs_hba *, enum ufs_pm_op);
  261. void (*dbg_register_dump)(struct ufs_hba *hba);
  262. };
  263. /* clock gating state */
  264. enum clk_gating_state {
  265. CLKS_OFF,
  266. CLKS_ON,
  267. REQ_CLKS_OFF,
  268. REQ_CLKS_ON,
  269. };
  270. /**
  271. * struct ufs_clk_gating - UFS clock gating related info
  272. * @gate_work: worker to turn off clocks after some delay as specified in
  273. * delay_ms
  274. * @ungate_work: worker to turn on clocks that will be used in case of
  275. * interrupt context
  276. * @state: the current clocks state
  277. * @delay_ms: gating delay in ms
  278. * @is_suspended: clk gating is suspended when set to 1 which can be used
  279. * during suspend/resume
  280. * @delay_attr: sysfs attribute to control delay_attr
  281. * @active_reqs: number of requests that are pending and should be waited for
  282. * completion before gating clocks.
  283. */
  284. struct ufs_clk_gating {
  285. struct delayed_work gate_work;
  286. struct work_struct ungate_work;
  287. enum clk_gating_state state;
  288. unsigned long delay_ms;
  289. bool is_suspended;
  290. struct device_attribute delay_attr;
  291. int active_reqs;
  292. };
  293. struct ufs_clk_scaling {
  294. ktime_t busy_start_t;
  295. bool is_busy_started;
  296. unsigned long tot_busy_t;
  297. unsigned long window_start_t;
  298. };
  299. /**
  300. * struct ufs_init_prefetch - contains data that is pre-fetched once during
  301. * initialization
  302. * @icc_level: icc level which was read during initialization
  303. */
  304. struct ufs_init_prefetch {
  305. u32 icc_level;
  306. };
  307. /**
  308. * struct ufs_hba - per adapter private structure
  309. * @mmio_base: UFSHCI base register address
  310. * @ucdl_base_addr: UFS Command Descriptor base address
  311. * @utrdl_base_addr: UTP Transfer Request Descriptor base address
  312. * @utmrdl_base_addr: UTP Task Management Descriptor base address
  313. * @ucdl_dma_addr: UFS Command Descriptor DMA address
  314. * @utrdl_dma_addr: UTRDL DMA address
  315. * @utmrdl_dma_addr: UTMRDL DMA address
  316. * @host: Scsi_Host instance of the driver
  317. * @dev: device handle
  318. * @lrb: local reference block
  319. * @lrb_in_use: lrb in use
  320. * @outstanding_tasks: Bits representing outstanding task requests
  321. * @outstanding_reqs: Bits representing outstanding transfer requests
  322. * @capabilities: UFS Controller Capabilities
  323. * @nutrs: Transfer Request Queue depth supported by controller
  324. * @nutmrs: Task Management Queue depth supported by controller
  325. * @ufs_version: UFS Version to which controller complies
  326. * @vops: pointer to variant specific operations
  327. * @priv: pointer to variant specific private data
  328. * @irq: Irq number of the controller
  329. * @active_uic_cmd: handle of active UIC command
  330. * @uic_cmd_mutex: mutex for uic command
  331. * @tm_wq: wait queue for task management
  332. * @tm_tag_wq: wait queue for free task management slots
  333. * @tm_slots_in_use: bit map of task management request slots in use
  334. * @pwr_done: completion for power mode change
  335. * @tm_condition: condition variable for task management
  336. * @ufshcd_state: UFSHCD states
  337. * @eh_flags: Error handling flags
  338. * @intr_mask: Interrupt Mask Bits
  339. * @ee_ctrl_mask: Exception event control mask
  340. * @is_powered: flag to check if HBA is powered
  341. * @is_init_prefetch: flag to check if data was pre-fetched in initialization
  342. * @init_prefetch_data: data pre-fetched during initialization
  343. * @eh_work: Worker to handle UFS errors that require s/w attention
  344. * @eeh_work: Worker to handle exception events
  345. * @errors: HBA errors
  346. * @uic_error: UFS interconnect layer error status
  347. * @saved_err: sticky error mask
  348. * @saved_uic_err: sticky UIC error mask
  349. * @dev_cmd: ufs device management command information
  350. * @last_dme_cmd_tstamp: time stamp of the last completed DME command
  351. * @auto_bkops_enabled: to track whether bkops is enabled in device
  352. * @vreg_info: UFS device voltage regulator information
  353. * @clk_list_head: UFS host controller clocks list node head
  354. * @pwr_info: holds current power mode
  355. * @max_pwr_info: keeps the device max valid pwm
  356. */
  357. struct ufs_hba {
  358. void __iomem *mmio_base;
  359. /* Virtual memory reference */
  360. struct utp_transfer_cmd_desc *ucdl_base_addr;
  361. struct utp_transfer_req_desc *utrdl_base_addr;
  362. struct utp_task_req_desc *utmrdl_base_addr;
  363. /* DMA memory reference */
  364. dma_addr_t ucdl_dma_addr;
  365. dma_addr_t utrdl_dma_addr;
  366. dma_addr_t utmrdl_dma_addr;
  367. struct Scsi_Host *host;
  368. struct device *dev;
  369. /*
  370. * This field is to keep a reference to "scsi_device" corresponding to
  371. * "UFS device" W-LU.
  372. */
  373. struct scsi_device *sdev_ufs_device;
  374. enum ufs_dev_pwr_mode curr_dev_pwr_mode;
  375. enum uic_link_state uic_link_state;
  376. /* Desired UFS power management level during runtime PM */
  377. enum ufs_pm_level rpm_lvl;
  378. /* Desired UFS power management level during system PM */
  379. enum ufs_pm_level spm_lvl;
  380. int pm_op_in_progress;
  381. struct ufshcd_lrb *lrb;
  382. unsigned long lrb_in_use;
  383. unsigned long outstanding_tasks;
  384. unsigned long outstanding_reqs;
  385. u32 capabilities;
  386. int nutrs;
  387. int nutmrs;
  388. u32 ufs_version;
  389. struct ufs_hba_variant_ops *vops;
  390. void *priv;
  391. unsigned int irq;
  392. bool is_irq_enabled;
  393. /* Interrupt aggregation support is broken */
  394. #define UFSHCD_QUIRK_BROKEN_INTR_AGGR UFS_BIT(0)
  395. /*
  396. * delay before each dme command is required as the unipro
  397. * layer has shown instabilities
  398. */
  399. #define UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS UFS_BIT(1)
  400. /*
  401. * If UFS host controller is having issue in processing LCC (Line
  402. * Control Command) coming from device then enable this quirk.
  403. * When this quirk is enabled, host controller driver should disable
  404. * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE
  405. * attribute of device to 0).
  406. */
  407. #define UFSHCD_QUIRK_BROKEN_LCC UFS_BIT(2)
  408. /*
  409. * The attribute PA_RXHSUNTERMCAP specifies whether or not the
  410. * inbound Link supports unterminated line in HS mode. Setting this
  411. * attribute to 1 fixes moving to HS gear.
  412. */
  413. #define UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP UFS_BIT(3)
  414. /*
  415. * This quirk needs to be enabled if the host contoller only allows
  416. * accessing the peer dme attributes in AUTO mode (FAST AUTO or
  417. * SLOW AUTO).
  418. */
  419. #define UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE UFS_BIT(4)
  420. /*
  421. * This quirk needs to be enabled if the host contoller doesn't
  422. * advertise the correct version in UFS_VER register. If this quirk
  423. * is enabled, standard UFS host driver will call the vendor specific
  424. * ops (get_ufs_hci_version) to get the correct version.
  425. */
  426. #define UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION UFS_BIT(5)
  427. unsigned int quirks; /* Deviations from standard UFSHCI spec. */
  428. wait_queue_head_t tm_wq;
  429. wait_queue_head_t tm_tag_wq;
  430. unsigned long tm_condition;
  431. unsigned long tm_slots_in_use;
  432. struct uic_command *active_uic_cmd;
  433. struct mutex uic_cmd_mutex;
  434. struct completion *uic_async_done;
  435. u32 ufshcd_state;
  436. u32 eh_flags;
  437. u32 intr_mask;
  438. u16 ee_ctrl_mask;
  439. bool is_powered;
  440. bool is_init_prefetch;
  441. struct ufs_init_prefetch init_prefetch_data;
  442. /* Work Queues */
  443. struct work_struct eh_work;
  444. struct work_struct eeh_work;
  445. /* HBA Errors */
  446. u32 errors;
  447. u32 uic_error;
  448. u32 saved_err;
  449. u32 saved_uic_err;
  450. /* Device management request data */
  451. struct ufs_dev_cmd dev_cmd;
  452. ktime_t last_dme_cmd_tstamp;
  453. /* Keeps information of the UFS device connected to this host */
  454. struct ufs_dev_info dev_info;
  455. bool auto_bkops_enabled;
  456. struct ufs_vreg_info vreg_info;
  457. struct list_head clk_list_head;
  458. bool wlun_dev_clr_ua;
  459. struct ufs_pa_layer_attr pwr_info;
  460. struct ufs_pwr_mode_info max_pwr_info;
  461. struct ufs_clk_gating clk_gating;
  462. /* Control to enable/disable host capabilities */
  463. u32 caps;
  464. /* Allow dynamic clk gating */
  465. #define UFSHCD_CAP_CLK_GATING (1 << 0)
  466. /* Allow hiberb8 with clk gating */
  467. #define UFSHCD_CAP_HIBERN8_WITH_CLK_GATING (1 << 1)
  468. /* Allow dynamic clk scaling */
  469. #define UFSHCD_CAP_CLK_SCALING (1 << 2)
  470. /* Allow auto bkops to enabled during runtime suspend */
  471. #define UFSHCD_CAP_AUTO_BKOPS_SUSPEND (1 << 3)
  472. /*
  473. * This capability allows host controller driver to use the UFS HCI's
  474. * interrupt aggregation capability.
  475. * CAUTION: Enabling this might reduce overall UFS throughput.
  476. */
  477. #define UFSHCD_CAP_INTR_AGGR (1 << 4)
  478. /*
  479. * This capability allows the device auto-bkops to be always enabled
  480. * except during suspend (both runtime and suspend).
  481. * Enabling this capability means that device will always be allowed
  482. * to do background operation when it's active but it might degrade
  483. * the performance of ongoing read/write operations.
  484. */
  485. #define UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND (1 << 5)
  486. struct devfreq *devfreq;
  487. struct ufs_clk_scaling clk_scaling;
  488. bool is_sys_suspended;
  489. };
  490. /* Returns true if clocks can be gated. Otherwise false */
  491. static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)
  492. {
  493. return hba->caps & UFSHCD_CAP_CLK_GATING;
  494. }
  495. static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)
  496. {
  497. return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
  498. }
  499. static inline int ufshcd_is_clkscaling_enabled(struct ufs_hba *hba)
  500. {
  501. return hba->caps & UFSHCD_CAP_CLK_SCALING;
  502. }
  503. static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)
  504. {
  505. return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
  506. }
  507. static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)
  508. {
  509. if ((hba->caps & UFSHCD_CAP_INTR_AGGR) &&
  510. !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR))
  511. return true;
  512. else
  513. return false;
  514. }
  515. #define ufshcd_writel(hba, val, reg) \
  516. writel((val), (hba)->mmio_base + (reg))
  517. #define ufshcd_readl(hba, reg) \
  518. readl((hba)->mmio_base + (reg))
  519. /**
  520. * ufshcd_rmwl - read modify write into a register
  521. * @hba - per adapter instance
  522. * @mask - mask to apply on read value
  523. * @val - actual value to write
  524. * @reg - register address
  525. */
  526. static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)
  527. {
  528. u32 tmp;
  529. tmp = ufshcd_readl(hba, reg);
  530. tmp &= ~mask;
  531. tmp |= (val & mask);
  532. ufshcd_writel(hba, tmp, reg);
  533. }
  534. int ufshcd_alloc_host(struct device *, struct ufs_hba **);
  535. void ufshcd_dealloc_host(struct ufs_hba *);
  536. int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int);
  537. void ufshcd_remove(struct ufs_hba *);
  538. /**
  539. * ufshcd_hba_stop - Send controller to reset state
  540. * @hba: per adapter instance
  541. */
  542. static inline void ufshcd_hba_stop(struct ufs_hba *hba)
  543. {
  544. ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
  545. }
  546. static inline void check_upiu_size(void)
  547. {
  548. BUILD_BUG_ON(ALIGNED_UPIU_SIZE <
  549. GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE);
  550. }
  551. /**
  552. * ufshcd_set_variant - set variant specific data to the hba
  553. * @hba - per adapter instance
  554. * @variant - pointer to variant specific data
  555. */
  556. static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)
  557. {
  558. BUG_ON(!hba);
  559. hba->priv = variant;
  560. }
  561. /**
  562. * ufshcd_get_variant - get variant specific data from the hba
  563. * @hba - per adapter instance
  564. */
  565. static inline void *ufshcd_get_variant(struct ufs_hba *hba)
  566. {
  567. BUG_ON(!hba);
  568. return hba->priv;
  569. }
  570. static inline bool ufshcd_keep_autobkops_enabled_except_suspend(
  571. struct ufs_hba *hba)
  572. {
  573. return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND;
  574. }
  575. extern int ufshcd_runtime_suspend(struct ufs_hba *hba);
  576. extern int ufshcd_runtime_resume(struct ufs_hba *hba);
  577. extern int ufshcd_runtime_idle(struct ufs_hba *hba);
  578. extern int ufshcd_system_suspend(struct ufs_hba *hba);
  579. extern int ufshcd_system_resume(struct ufs_hba *hba);
  580. extern int ufshcd_shutdown(struct ufs_hba *hba);
  581. extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
  582. u8 attr_set, u32 mib_val, u8 peer);
  583. extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
  584. u32 *mib_val, u8 peer);
  585. /* UIC command interfaces for DME primitives */
  586. #define DME_LOCAL 0
  587. #define DME_PEER 1
  588. #define ATTR_SET_NOR 0 /* NORMAL */
  589. #define ATTR_SET_ST 1 /* STATIC */
  590. static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,
  591. u32 mib_val)
  592. {
  593. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  594. mib_val, DME_LOCAL);
  595. }
  596. static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,
  597. u32 mib_val)
  598. {
  599. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  600. mib_val, DME_LOCAL);
  601. }
  602. static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,
  603. u32 mib_val)
  604. {
  605. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,
  606. mib_val, DME_PEER);
  607. }
  608. static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,
  609. u32 mib_val)
  610. {
  611. return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,
  612. mib_val, DME_PEER);
  613. }
  614. static inline int ufshcd_dme_get(struct ufs_hba *hba,
  615. u32 attr_sel, u32 *mib_val)
  616. {
  617. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);
  618. }
  619. static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,
  620. u32 attr_sel, u32 *mib_val)
  621. {
  622. return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);
  623. }
  624. int ufshcd_hold(struct ufs_hba *hba, bool async);
  625. void ufshcd_release(struct ufs_hba *hba);
  626. /* Wrapper functions for safely calling variant operations */
  627. static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
  628. {
  629. if (hba->vops)
  630. return hba->vops->name;
  631. return "";
  632. }
  633. static inline int ufshcd_vops_init(struct ufs_hba *hba)
  634. {
  635. if (hba->vops && hba->vops->init)
  636. return hba->vops->init(hba);
  637. return 0;
  638. }
  639. static inline void ufshcd_vops_exit(struct ufs_hba *hba)
  640. {
  641. if (hba->vops && hba->vops->exit)
  642. return hba->vops->exit(hba);
  643. }
  644. static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba)
  645. {
  646. if (hba->vops && hba->vops->get_ufs_hci_version)
  647. return hba->vops->get_ufs_hci_version(hba);
  648. return ufshcd_readl(hba, REG_UFS_VERSION);
  649. }
  650. static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba,
  651. bool up, enum ufs_notify_change_status status)
  652. {
  653. if (hba->vops && hba->vops->clk_scale_notify)
  654. return hba->vops->clk_scale_notify(hba, up, status);
  655. return 0;
  656. }
  657. static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on)
  658. {
  659. if (hba->vops && hba->vops->setup_clocks)
  660. return hba->vops->setup_clocks(hba, on);
  661. return 0;
  662. }
  663. static inline int ufshcd_vops_setup_regulators(struct ufs_hba *hba, bool status)
  664. {
  665. if (hba->vops && hba->vops->setup_regulators)
  666. return hba->vops->setup_regulators(hba, status);
  667. return 0;
  668. }
  669. static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba,
  670. bool status)
  671. {
  672. if (hba->vops && hba->vops->hce_enable_notify)
  673. return hba->vops->hce_enable_notify(hba, status);
  674. return 0;
  675. }
  676. static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba,
  677. bool status)
  678. {
  679. if (hba->vops && hba->vops->link_startup_notify)
  680. return hba->vops->link_startup_notify(hba, status);
  681. return 0;
  682. }
  683. static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba,
  684. bool status,
  685. struct ufs_pa_layer_attr *dev_max_params,
  686. struct ufs_pa_layer_attr *dev_req_params)
  687. {
  688. if (hba->vops && hba->vops->pwr_change_notify)
  689. return hba->vops->pwr_change_notify(hba, status,
  690. dev_max_params, dev_req_params);
  691. return -ENOTSUPP;
  692. }
  693. static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op)
  694. {
  695. if (hba->vops && hba->vops->suspend)
  696. return hba->vops->suspend(hba, op);
  697. return 0;
  698. }
  699. static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op)
  700. {
  701. if (hba->vops && hba->vops->resume)
  702. return hba->vops->resume(hba, op);
  703. return 0;
  704. }
  705. static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba)
  706. {
  707. if (hba->vops && hba->vops->dbg_register_dump)
  708. hba->vops->dbg_register_dump(hba);
  709. }
  710. #endif /* End of Header */