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