ufshcd.h 25 KB

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