blob: 9a538174fe4ec328df5508bbbd27e1536cab8798 [file] [log] [blame]
Hamad Kadmany090709b2013-01-06 12:08:13 +02001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider435ad8e2011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider435ad8e2011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmany090709b2013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider435ad8e2011-12-14 16:53:30 +020029#include <linux/delay.h> /* msleep */
30#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020031#include <linux/clk.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020032#include <linux/poll.h> /* poll() file op */
33#include <linux/wait.h> /* wait() macros, sleeping */
34#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020035#include <linux/bitops.h> /* BIT() macro */
Joel Nider435ad8e2011-12-14 16:53:30 +020036#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020037#include <mach/gpio.h>
Joel Nider435ad8e2011-12-14 16:53:30 +020038#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany81cee052012-11-29 14:15:57 +020039#include <linux/timer.h> /* Timer services */
40#include <linux/jiffies.h> /* Jiffies counter */
Joel Nider5556a852011-10-16 10:52:13 +020041#include <mach/dma.h>
42#include <mach/msm_tspp.h>
Joel Nider5556a852011-10-16 10:52:13 +020043#include <linux/debugfs.h>
Liron Kuch59339922013-01-01 18:29:47 +020044#include <linux/of.h>
45#include <linux/of_gpio.h>
Liron Kuch275c0b32013-02-10 15:19:32 +020046#include <linux/string.h>
Joel Nider5556a852011-10-16 10:52:13 +020047
48/*
49 * General defines
50 */
Joel Nider5556a852011-10-16 10:52:13 +020051#define TSPP_TSIF_INSTANCES 2
Liron Kuch275c0b32013-02-10 15:19:32 +020052#define TSPP_GPIOS_PER_TSIF 4
Joel Nider5556a852011-10-16 10:52:13 +020053#define TSPP_FILTER_TABLES 3
Joel Nider435ad8e2011-12-14 16:53:30 +020054#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020055#define TSPP_NUM_CHANNELS 16
56#define TSPP_NUM_PRIORITIES 16
57#define TSPP_NUM_KEYS 8
58#define INVALID_CHANNEL 0xFFFFFFFF
Hamad Kadmany81cee052012-11-29 14:15:57 +020059
60/*
61 * BAM descriptor FIFO size (in number of descriptors).
62 * Max number of descriptors allowed by SPS which is 8K-1.
63 * Restrict it to half of this to save DMA memory.
64 */
65#define TSPP_SPS_DESCRIPTOR_COUNT (4 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020066#define TSPP_PACKET_LENGTH 188
67#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany81cee052012-11-29 14:15:57 +020068
69/* Max descriptor buffer size allowed by SPS */
70#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
71
72/*
Hamad Kadmany090709b2013-01-06 12:08:13 +020073 * Returns whether to use DMA pool for TSPP output buffers.
74 * For buffers smaller than page size, using DMA pool
75 * provides better memory utilization as dma_alloc_coherent
76 * allocates minimum of page size.
77 */
78#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
79
80/*
Hamad Kadmany81cee052012-11-29 14:15:57 +020081 * Max allowed TSPP buffers/descriptors.
82 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
83 */
84#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020085#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
86#define SPS_DESCRIPTOR_SIZE 8
87#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider435ad8e2011-12-14 16:53:30 +020088#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020089
90/*
91 * TSIF register offsets
92 */
93#define TSIF_STS_CTL_OFF (0x0)
94#define TSIF_TIME_LIMIT_OFF (0x4)
95#define TSIF_CLK_REF_OFF (0x8)
96#define TSIF_LPBK_FLAGS_OFF (0xc)
97#define TSIF_LPBK_DATA_OFF (0x10)
98#define TSIF_TEST_CTL_OFF (0x14)
99#define TSIF_TEST_MODE_OFF (0x18)
100#define TSIF_TEST_RESET_OFF (0x1c)
101#define TSIF_TEST_EXPORT_OFF (0x20)
102#define TSIF_TEST_CURRENT_OFF (0x24)
103
104#define TSIF_DATA_PORT_OFF (0x100)
105
106/* bits for TSIF_STS_CTL register */
107#define TSIF_STS_CTL_EN_IRQ BIT(28)
108#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
109#define TSIF_STS_CTL_1ST_PACKET BIT(26)
110#define TSIF_STS_CTL_OVERFLOW BIT(25)
111#define TSIF_STS_CTL_LOST_SYNC BIT(24)
112#define TSIF_STS_CTL_TIMEOUT BIT(23)
113#define TSIF_STS_CTL_INV_SYNC BIT(21)
114#define TSIF_STS_CTL_INV_NULL BIT(20)
115#define TSIF_STS_CTL_INV_ERROR BIT(19)
116#define TSIF_STS_CTL_INV_ENABLE BIT(18)
117#define TSIF_STS_CTL_INV_DATA BIT(17)
118#define TSIF_STS_CTL_INV_CLOCK BIT(16)
119#define TSIF_STS_CTL_SPARE BIT(15)
120#define TSIF_STS_CTL_EN_NULL BIT(11)
121#define TSIF_STS_CTL_EN_ERROR BIT(10)
122#define TSIF_STS_CTL_LAST_BIT BIT(9)
123#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
124#define TSIF_STS_CTL_EN_TCR BIT(7)
125#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider435ad8e2011-12-14 16:53:30 +0200126#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200127#define TSIF_STS_CTL_EN_DM BIT(4)
128#define TSIF_STS_CTL_STOP BIT(3)
129#define TSIF_STS_CTL_START BIT(0)
130
131/*
132 * TSPP register offsets
133 */
Liron Kuch72b78552012-10-30 17:47:50 +0200134#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200135#define TSPP_CLK_CONTROL 0x04
Liron Kuch72b78552012-10-30 17:47:50 +0200136#define TSPP_CONFIG 0x08
137#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200138#define TSPP_PS_DISABLE 0x10
Liron Kuch72b78552012-10-30 17:47:50 +0200139#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200140#define TSPP_MSG_IRQ_MASK 0x18
141#define TSPP_IRQ_STATUS 0x1C
142#define TSPP_IRQ_MASK 0x20
143#define TSPP_IRQ_CLEAR 0x24
144#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch72b78552012-10-30 17:47:50 +0200145#define TSPP_STATUS 0x68
146#define TSPP_CURR_TSP_HEADER 0x6C
147#define TSPP_CURR_PID_FILTER 0x70
148#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
149#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
150#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200151#define TSPP_KEY_VALID 0xA0
152#define TSPP_KEY_ERROR 0xA4
153#define TSPP_TEST_CTRL 0xA8
Liron Kuch72b78552012-10-30 17:47:50 +0200154#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200155#define TSPP_GENERICS 0xB0
Liron Kuch72b78552012-10-30 17:47:50 +0200156#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200157
158/*
159 * Register bit definitions
160 */
161/* TSPP_RST */
162#define TSPP_RST_RESET BIT(0)
163
164/* TSPP_CLK_CONTROL */
165#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
166#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
167#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
168#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
169#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
170#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
171#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
172#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
173#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
174#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
175
176/* TSPP_CONFIG */
177#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
178((_b & 0xF) << 8))
179#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
180#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
181#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
182#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
183#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
184#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
185#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
186#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
187#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
188
189/* TSPP_CONTROL */
190#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
191#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
192#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
193#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
194#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
195#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
196
197/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
198#define TSPP_MSG_TSPP_IRQ BIT(2)
199#define TSPP_MSG_TSIF_1_IRQ BIT(1)
200#define TSPP_MSG_TSIF_0_IRQ BIT(0)
201
202/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch72b78552012-10-30 17:47:50 +0200203#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
204#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200205#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
206#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
207#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
208
209/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200210#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
211#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200212#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch72b78552012-10-30 17:47:50 +0200213#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200214
215/* TSPP_STATUS */
Liron Kuch72b78552012-10-30 17:47:50 +0200216#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
217#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
218#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
219#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200220
221/* TSPP_GENERICS */
Liron Kuch72b78552012-10-30 17:47:50 +0200222#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200223#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch72b78552012-10-30 17:47:50 +0200224#define TSPP_GENERICS_MAX_PIPES BIT(2)
225#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
226#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200227
228/*
229 * TSPP memory regions
230 */
231#define TSPP_PID_FILTER_TABLE0 0x800
232#define TSPP_PID_FILTER_TABLE1 0x880
233#define TSPP_PID_FILTER_TABLE2 0x900
234#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
235#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
236#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
237#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
238#define TSPP_DATA_KEY 0xCD0
239
Joel Nider5556a852011-10-16 10:52:13 +0200240struct debugfs_entry {
241 const char *name;
242 mode_t mode;
243 int offset;
244};
245
246static const struct debugfs_entry debugfs_tsif_regs[] = {
247 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
248 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
249 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
250 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
251 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
252 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
253 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
254 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
255 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
256 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
257 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
258};
259
260static const struct debugfs_entry debugfs_tspp_regs[] = {
261 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
262 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
263 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
264 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
265 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
266 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
267 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
268 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
269 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
270 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
271 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
272 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
273 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
274 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
275 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
276 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
277 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
278 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
279 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
280 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
281 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
282 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
283 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
284 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
285 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
286 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
287 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
288 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
289 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
290};
291
Joel Nider5556a852011-10-16 10:52:13 +0200292struct tspp_pid_filter {
293 u32 filter; /* see FILTER_ macros */
294 u32 config; /* see FILTER_ macros */
295};
296
297/* tsp_info */
298#define FILTER_HEADER_ERROR_MASK BIT(7)
299#define FILTER_TRANS_END_DISABLE BIT(6)
300#define FILTER_DEC_ON_ERROR_EN BIT(5)
301#define FILTER_DECRYPT BIT(4)
302#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
303#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
304#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
305 (_p->config & ~0xF) | (_b & 0xF))
306#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
307#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
308 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
309#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
310#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
311 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
312#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
313#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
314 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
315#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
316#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
317 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
318#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
319#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
320 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
321
322struct tspp_global_performance_regs {
323 u32 tsp_total;
324 u32 tsp_ignored;
325 u32 tsp_error;
326 u32 tsp_sync;
327};
328
329struct tspp_pipe_context_regs {
330 u16 pes_bytes_left;
331 u16 count;
332 u32 tsif_suffix;
333} __packed;
334#define CONTEXT_GET_STATE(_a) (_a & 0x3)
335#define CONTEXT_UNSPEC_LENGTH BIT(11)
336#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
337
Hamad Kadmany81cee052012-11-29 14:15:57 +0200338#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
339
Joel Nider5556a852011-10-16 10:52:13 +0200340struct tspp_pipe_performance_regs {
341 u32 tsp_total;
342 u32 ps_duplicate_tsp;
343 u32 tsp_no_payload;
344 u32 tsp_broken_ps;
345 u32 ps_total_num;
346 u32 ps_continuity_error;
347 u32 ps_length_error;
348 u32 pes_sync_error;
349};
350
351struct tspp_tsif_device {
352 void __iomem *base;
353 u32 time_limit;
354 u32 ref_count;
Joel Nider435ad8e2011-12-14 16:53:30 +0200355 enum tspp_tsif_mode mode;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200356 int clock_inverse;
357 int data_inverse;
358 int sync_inverse;
359 int enable_inverse;
Hamad Kadmany44307d32012-11-25 09:49:51 +0200360 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200361
362 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200363 struct dentry *dent_tsif;
364 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmany44307d32012-11-25 09:49:51 +0200365 u32 stat_rx;
366 u32 stat_overflow;
367 u32 stat_lost_sync;
368 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200369};
370
371enum tspp_buf_state {
372 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
373 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider435ad8e2011-12-14 16:53:30 +0200374 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
375 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200376};
377
378struct tspp_mem_buffer {
Joel Nider435ad8e2011-12-14 16:53:30 +0200379 struct tspp_mem_buffer *next;
380 struct sps_mem_buffer sps;
381 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200382 enum tspp_buf_state state;
383 size_t filled; /* how much data this buffer is holding */
384 int read_index; /* where to start reading data from */
385};
386
387/* this represents each char device 'channel' */
388struct tspp_channel {
389 struct cdev cdev;
390 struct device *dd;
Joel Nider435ad8e2011-12-14 16:53:30 +0200391 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200392 struct sps_pipe *pipe;
393 struct sps_connect config;
394 struct sps_register_event event;
Joel Nider435ad8e2011-12-14 16:53:30 +0200395 struct tspp_mem_buffer *data; /* list of buffers */
396 struct tspp_mem_buffer *read; /* first buffer ready to be read */
397 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
398 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200399 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider435ad8e2011-12-14 16:53:30 +0200400 u32 id; /* channel id (0-15) */
401 int used; /* is this channel in use? */
402 int key; /* which encryption key index is used */
403 u32 buffer_size; /* size of the sps transfer buffers */
404 u32 max_buffers; /* how many buffers should be allocated */
405 u32 buffer_count; /* how many buffers are actually allocated */
406 u32 filter_count; /* how many filters have been added to this channel */
407 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200408 enum tspp_source src;
409 enum tspp_mode mode;
Joel Nider435ad8e2011-12-14 16:53:30 +0200410 tspp_notifier *notifier; /* used only with kernel api */
411 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany81cee052012-11-29 14:15:57 +0200412 u32 expiration_period_ms; /* notification on partially filled buffers */
413 struct timer_list expiration_timer;
Hamad Kadmany090709b2013-01-06 12:08:13 +0200414 struct dma_pool *dma_pool;
Liron Kuch72b78552012-10-30 17:47:50 +0200415 tspp_memfree *memfree; /* user defined memory free function */
416 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200417};
418
419struct tspp_pid_filter_table {
420 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
421};
422
423struct tspp_key_entry {
424 u32 even_lsb;
425 u32 even_msb;
426 u32 odd_lsb;
427 u32 odd_msb;
428};
429
430struct tspp_key_table {
431 struct tspp_key_entry entry[TSPP_NUM_KEYS];
432};
433
Joel Nider435ad8e2011-12-14 16:53:30 +0200434/* this represents the actual hardware device */
435struct tspp_device {
436 struct list_head devlist; /* list of all devices */
437 struct platform_device *pdev;
438 void __iomem *base;
439 unsigned int tspp_irq;
440 unsigned int bam_irq;
441 u32 bam_handle;
442 struct sps_bam_props bam_props;
443 struct wake_lock wake_lock;
444 spinlock_t spinlock;
445 struct tasklet_struct tlet;
446 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
447 /* clocks */
448 struct clk *tsif_pclk;
449 struct clk *tsif_ref_clk;
450 /* data */
451 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
452 struct tspp_channel channels[TSPP_NUM_CHANNELS];
453 struct tspp_key_table *tspp_key_table;
454 struct tspp_global_performance_regs *tspp_global_performance;
455 struct tspp_pipe_context_regs *tspp_pipe_context;
456 struct tspp_pipe_performance_regs *tspp_pipe_performance;
457
458 struct dentry *dent;
459 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
460};
461
462
Joel Nider5556a852011-10-16 10:52:13 +0200463static struct class *tspp_class;
464static int tspp_key_entry;
465static dev_t tspp_minor; /* next minor number to assign */
Joel Nider435ad8e2011-12-14 16:53:30 +0200466
467static LIST_HEAD(tspp_devices);
468
469/* forward declarations */
470static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
471static ssize_t tspp_open(struct inode *inode, struct file *filp);
472static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
473static ssize_t tspp_release(struct inode *inode, struct file *filp);
474static long tspp_ioctl(struct file *, unsigned int, unsigned long);
475
476/* file operations */
477static const struct file_operations tspp_fops = {
478 .owner = THIS_MODULE,
479 .read = tspp_read,
480 .open = tspp_open,
481 .poll = tspp_poll,
482 .release = tspp_release,
483 .unlocked_ioctl = tspp_ioctl,
484};
Joel Nider5556a852011-10-16 10:52:13 +0200485
486/*** IRQ ***/
Joel Nider435ad8e2011-12-14 16:53:30 +0200487static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200488{
Joel Nider435ad8e2011-12-14 16:53:30 +0200489 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200490 u32 status, mask;
491 u32 data;
492
493 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
494 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
495 status &= mask;
496
497 if (!status) {
498 dev_warn(&device->pdev->dev, "Spurious interrupt");
499 return IRQ_NONE;
500 }
501
502 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
503
504 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
505 /* read the key error info */
506 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
507 dev_info(&device->pdev->dev, "key error 0x%x", data);
508 }
509 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
510 data = readl_relaxed(device->base + TSPP_KEY_VALID);
511 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
512 }
513 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
514 dev_info(&device->pdev->dev, "key switched");
515
516 if (status & 0xffff)
Joel Nider435ad8e2011-12-14 16:53:30 +0200517 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200518
519 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmany44307d32012-11-25 09:49:51 +0200520
521 /*
522 * Before returning IRQ_HANDLED to the generic interrupt handling
523 * framework need to make sure all operations including clearing of
524 * interrupt status registers in the hardware is performed.
525 * Thus a barrier after clearing the interrupt status register
526 * is required to guarantee that the interrupt status register has
527 * really been cleared by the time we return from this handler.
528 */
529 wmb();
530 return IRQ_HANDLED;
531}
532
533static irqreturn_t tsif_isr(int irq, void *dev)
534{
535 struct tspp_tsif_device *tsif_device = dev;
536 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
537
538 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
539 TSIF_STS_CTL_OVERFLOW |
540 TSIF_STS_CTL_LOST_SYNC |
541 TSIF_STS_CTL_TIMEOUT)))
542 return IRQ_NONE;
543
544 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
545 tsif_device->stat_overflow++;
546
547 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
548 tsif_device->stat_lost_sync++;
549
550 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
551 tsif_device->stat_timeout++;
552
553 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
554
555 /*
556 * Before returning IRQ_HANDLED to the generic interrupt handling
557 * framework need to make sure all operations including clearing of
558 * interrupt status registers in the hardware is performed.
559 * Thus a barrier after clearing the interrupt status register
560 * is required to guarantee that the interrupt status register has
561 * really been cleared by the time we return from this handler.
562 */
Joel Nider5556a852011-10-16 10:52:13 +0200563 wmb();
564 return IRQ_HANDLED;
565}
566
567/*** callbacks ***/
568static void tspp_sps_complete_cb(struct sps_event_notify *notify)
569{
Joel Nider435ad8e2011-12-14 16:53:30 +0200570 struct tspp_device *pdev = notify->user;
571 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200572}
573
Hamad Kadmany81cee052012-11-29 14:15:57 +0200574static void tspp_expiration_timer(unsigned long data)
575{
576 struct tspp_device *pdev = (struct tspp_device *)data;
577
578 if (pdev)
579 tasklet_schedule(&pdev->tlet);
580}
581
Joel Nider5556a852011-10-16 10:52:13 +0200582/*** tasklet ***/
583static void tspp_sps_complete_tlet(unsigned long data)
584{
585 int i;
586 int complete;
587 unsigned long flags;
588 struct sps_iovec iovec;
589 struct tspp_channel *channel;
590 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200591 spin_lock_irqsave(&device->spinlock, flags);
592
593 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
594 complete = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +0200595 channel = &device->channels[i];
Hamad Kadmany81cee052012-11-29 14:15:57 +0200596
Joel Nider435ad8e2011-12-14 16:53:30 +0200597 if (!channel->used || !channel->waiting)
598 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200599
Hamad Kadmany81cee052012-11-29 14:15:57 +0200600 /* stop the expiration timer */
601 if (channel->expiration_period_ms)
602 del_timer(&channel->expiration_timer);
603
Joel Nider5556a852011-10-16 10:52:13 +0200604 /* get completions */
Joel Nider435ad8e2011-12-14 16:53:30 +0200605 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200606 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
607 pr_err("tspp: Error in iovec on channel %i",
608 channel->id);
609 break;
610 }
611 if (iovec.size == 0)
612 break;
613
Joel Nider435ad8e2011-12-14 16:53:30 +0200614 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200615 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider435ad8e2011-12-14 16:53:30 +0200616 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200617
618 complete = 1;
Joel Nider435ad8e2011-12-14 16:53:30 +0200619 channel->waiting->state = TSPP_BUF_STATE_DATA;
620 channel->waiting->filled = iovec.size;
621 channel->waiting->read_index = 0;
622
Hamad Kadmany44307d32012-11-25 09:49:51 +0200623 if (channel->src == TSPP_SOURCE_TSIF0)
624 device->tsif[0].stat_rx++;
625 else if (channel->src == TSPP_SOURCE_TSIF1)
626 device->tsif[1].stat_rx++;
627
Joel Nider435ad8e2011-12-14 16:53:30 +0200628 /* update the pointers */
629 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200630 }
631
Joel Nider435ad8e2011-12-14 16:53:30 +0200632 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200633 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200634 wake_up_interruptible(&channel->in_queue);
Joel Nider435ad8e2011-12-14 16:53:30 +0200635
636 /* call notifiers */
637 if (channel->notifier)
638 channel->notifier(channel->id,
639 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200640 }
Hamad Kadmany81cee052012-11-29 14:15:57 +0200641
642 /* restart expiration timer */
643 if (channel->expiration_period_ms)
644 mod_timer(&channel->expiration_timer,
645 jiffies +
646 MSEC_TO_JIFFIES(
647 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200648 }
649
650 spin_unlock_irqrestore(&device->spinlock, flags);
651}
652
653/*** GPIO functions ***/
Liron Kuch275c0b32013-02-10 15:19:32 +0200654static int tspp_gpios_disable(const struct tspp_tsif_device *tsif_device,
655 const struct msm_gpio *table,
656 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200657{
658 int rc = 0;
659 int i;
660 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200661
Joel Nider5556a852011-10-16 10:52:13 +0200662 for (i = size-1; i >= 0; i--) {
663 int tmp;
664 g = table + i;
Liron Kuch59339922013-01-01 18:29:47 +0200665
Liron Kuch275c0b32013-02-10 15:19:32 +0200666 /* don't use sync GPIO when not working in mode 2 */
667 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
668 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
669 continue;
670
Liron Kuch59339922013-01-01 18:29:47 +0200671 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
672 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
673 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200674 if (tmp) {
Liron Kuch72b78552012-10-30 17:47:50 +0200675 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200676 g->gpio_cfg, g->label ?: "?", rc);
677 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
678 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
679 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
680 GPIO_DRVSTR(g->gpio_cfg));
681 if (!rc)
682 rc = tmp;
683 }
684 }
685
686 return rc;
687}
688
Liron Kuch275c0b32013-02-10 15:19:32 +0200689static int tspp_gpios_enable(const struct tspp_tsif_device *tsif_device,
690 const struct msm_gpio *table,
691 int size)
Joel Nider5556a852011-10-16 10:52:13 +0200692{
693 int rc;
Liron Kuch275c0b32013-02-10 15:19:32 +0200694 int i;
Joel Nider5556a852011-10-16 10:52:13 +0200695 const struct msm_gpio *g;
Liron Kuch59339922013-01-01 18:29:47 +0200696
Joel Nider5556a852011-10-16 10:52:13 +0200697 for (i = 0; i < size; i++) {
698 g = table + i;
Liron Kuch275c0b32013-02-10 15:19:32 +0200699
700 /* don't use sync GPIO when not working in mode 2 */
701 if ((tsif_device->mode != TSPP_TSIF_MODE_2) &&
702 (strnstr(g->label, "sync", strlen(g->label)) != NULL))
703 continue;
704
Joel Nider5556a852011-10-16 10:52:13 +0200705 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
706 if (rc) {
Liron Kuch72b78552012-10-30 17:47:50 +0200707 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200708 g->gpio_cfg, g->label ?: "?", rc);
709 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
710 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
711 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
712 GPIO_DRVSTR(g->gpio_cfg));
713 goto err;
714 }
715 }
716 return 0;
717err:
Liron Kuch275c0b32013-02-10 15:19:32 +0200718 tspp_gpios_disable(tsif_device, table, i);
Joel Nider5556a852011-10-16 10:52:13 +0200719
Joel Nider5556a852011-10-16 10:52:13 +0200720 return rc;
721}
722
Liron Kuch275c0b32013-02-10 15:19:32 +0200723
724static int tspp_config_gpios(struct tspp_device *device,
725 enum tspp_source source,
726 int enable)
Joel Nider5556a852011-10-16 10:52:13 +0200727{
Liron Kuch275c0b32013-02-10 15:19:32 +0200728 const struct msm_gpio *table;
729 struct msm_tspp_platform_data *pdata = device->pdev->dev.platform_data;
730 int num_gpios = (pdata->num_gpios / TSPP_TSIF_INSTANCES);
731 int i = 0;
Liron Kuch59339922013-01-01 18:29:47 +0200732
Liron Kuch275c0b32013-02-10 15:19:32 +0200733 if (num_gpios != TSPP_GPIOS_PER_TSIF) {
734 pr_err("tspp %s: unexpected number of GPIOs %d, expected %d\n",
735 __func__, num_gpios, TSPP_GPIOS_PER_TSIF);
736 return -EINVAL;
737 }
Joel Nider5556a852011-10-16 10:52:13 +0200738
Liron Kuch275c0b32013-02-10 15:19:32 +0200739 /*
740 * Note: this code assumes that the GPIO definitions in the
741 * pdata->gpios table are according to the TSIF instance number,
742 * i.e., that TSIF0 GPIOs are defined first, then TSIF1 GPIOs etc.
743 */
744 switch (source) {
745 case TSPP_SOURCE_TSIF0:
746 i = 0;
747 break;
748 case TSPP_SOURCE_TSIF1:
749 i = 1;
750 break;
751 default:
752 pr_err("tspp %s: invalid source\n", __func__);
753 return -EINVAL;
754 }
Liron Kuch59339922013-01-01 18:29:47 +0200755
Liron Kuch275c0b32013-02-10 15:19:32 +0200756 table = pdata->gpios + (i * num_gpios);
757 if (enable)
758 return tspp_gpios_enable(&device->tsif[i], table, num_gpios);
759 else
760 return tspp_gpios_disable(&device->tsif[i], table, num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200761}
762
Joel Nider435ad8e2011-12-14 16:53:30 +0200763/*** Clock functions ***/
764static int tspp_clock_start(struct tspp_device *device)
765{
766 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
767 pr_err("tspp: Can't start pclk");
768 return -EBUSY;
769 }
770
771 if (device->tsif_ref_clk &&
772 clk_prepare_enable(device->tsif_ref_clk) != 0) {
773 pr_err("tspp: Can't start ref clk");
774 clk_disable_unprepare(device->tsif_pclk);
775 return -EBUSY;
776 }
777
778 return 0;
779}
780
781static void tspp_clock_stop(struct tspp_device *device)
782{
783 if (device->tsif_pclk)
784 clk_disable(device->tsif_pclk);
785
786 if (device->tsif_ref_clk)
787 clk_disable(device->tsif_ref_clk);
788}
789
Joel Nider5556a852011-10-16 10:52:13 +0200790/*** TSIF functions ***/
791static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
792{
793 int start_hardware = 0;
794 u32 ctl;
795
796 if (tsif_device->ref_count == 0) {
797 start_hardware = 1;
798 } else if (tsif_device->ref_count > 0) {
799 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
800 if ((ctl & TSIF_STS_CTL_START) != 1) {
801 /* this hardware should already be running */
802 pr_warn("tspp: tsif hw not started but ref count > 0");
803 start_hardware = 1;
804 }
805 }
806
807 if (start_hardware) {
Joel Nider435ad8e2011-12-14 16:53:30 +0200808 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200809 TSIF_STS_CTL_EN_DM;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200810
811 if (tsif_device->clock_inverse)
812 ctl |= TSIF_STS_CTL_INV_CLOCK;
813
814 if (tsif_device->data_inverse)
815 ctl |= TSIF_STS_CTL_INV_DATA;
816
817 if (tsif_device->sync_inverse)
818 ctl |= TSIF_STS_CTL_INV_SYNC;
819
820 if (tsif_device->enable_inverse)
821 ctl |= TSIF_STS_CTL_INV_ENABLE;
822
Joel Nider435ad8e2011-12-14 16:53:30 +0200823 switch (tsif_device->mode) {
824 case TSPP_TSIF_MODE_LOOPBACK:
825 ctl |= TSIF_STS_CTL_EN_NULL |
826 TSIF_STS_CTL_EN_ERROR |
827 TSIF_STS_CTL_TEST_MODE;
828 break;
829 case TSPP_TSIF_MODE_1:
830 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
831 TSIF_STS_CTL_EN_TCR;
832 break;
833 case TSPP_TSIF_MODE_2:
834 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
835 TSIF_STS_CTL_EN_TCR |
836 TSIF_STS_CTL_MODE_2;
837 break;
838 default:
839 pr_warn("tspp: unknown tsif mode 0x%x",
840 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200841 }
842 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
843 writel_relaxed(tsif_device->time_limit,
844 tsif_device->base + TSIF_TIME_LIMIT_OFF);
845 wmb();
846 writel_relaxed(ctl | TSIF_STS_CTL_START,
847 tsif_device->base + TSIF_STS_CTL_OFF);
848 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200849 }
850
Joel Nider435ad8e2011-12-14 16:53:30 +0200851 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200852 tsif_device->ref_count++;
853
Joel Nider435ad8e2011-12-14 16:53:30 +0200854 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200855}
856
857static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
858{
859 if (tsif_device->ref_count == 0)
860 return;
861
862 tsif_device->ref_count--;
863
864 if (tsif_device->ref_count == 0) {
865 writel_relaxed(TSIF_STS_CTL_STOP,
866 tsif_device->base + TSIF_STS_CTL_OFF);
867 wmb();
868 }
869}
870
Joel Nider435ad8e2011-12-14 16:53:30 +0200871/*** local TSPP functions ***/
872static int tspp_channels_in_use(struct tspp_device *pdev)
873{
874 int i;
875 int count = 0;
876 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
877 count += (pdev->channels[i].used ? 1 : 0);
878
879 return count;
880}
881
882static struct tspp_device *tspp_find_by_id(int id)
883{
884 struct tspp_device *dev;
885 list_for_each_entry(dev, &tspp_devices, devlist) {
886 if (dev->pdev->id == id)
887 return dev;
888 }
889 return NULL;
890}
891
Joel Nider5556a852011-10-16 10:52:13 +0200892static int tspp_get_key_entry(void)
893{
894 int i;
895 for (i = 0; i < TSPP_NUM_KEYS; i++) {
896 if (!(tspp_key_entry & (1 << i))) {
897 tspp_key_entry |= (1 << i);
898 return i;
899 }
900 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200901 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200902}
903
904static void tspp_free_key_entry(int entry)
905{
906 if (entry > TSPP_NUM_KEYS) {
907 pr_err("tspp_free_key_entry: index out of bounds");
908 return;
909 }
910
911 tspp_key_entry &= ~(1 << entry);
912}
913
Joel Nider435ad8e2011-12-14 16:53:30 +0200914static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +0200915 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200916{
Joel Nider435ad8e2011-12-14 16:53:30 +0200917 if (size < TSPP_MIN_BUFFER_SIZE ||
918 size > TSPP_MAX_BUFFER_SIZE) {
919 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200920 return -ENOMEM;
921 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200922
923 if (alloc) {
924 TSPP_DEBUG("tspp using alloc function");
925 desc->virt_base = alloc(channel_id, size,
926 &desc->phys_base, user);
927 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200928 if (!dma_pool)
929 desc->virt_base = dma_alloc_coherent(NULL, size,
930 &desc->phys_base, GFP_KERNEL);
931 else
932 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
933 &desc->phys_base);
934
Liron Kuch72b78552012-10-30 17:47:50 +0200935 if (desc->virt_base == 0) {
Hamad Kadmany090709b2013-01-06 12:08:13 +0200936 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch72b78552012-10-30 17:47:50 +0200937 return -ENOMEM;
938 }
Joel Nider435ad8e2011-12-14 16:53:30 +0200939 }
940
941 desc->size = size;
942 return 0;
943}
944
945static int tspp_queue_buffer(struct tspp_channel *channel,
946 struct tspp_mem_buffer *buffer)
947{
948 int rc;
949 u32 flags = 0;
950
951 /* make sure the interrupt frequency is valid */
952 if (channel->int_freq < 1)
953 channel->int_freq = 1;
954
955 /* generate interrupt according to requested frequency */
956 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany81cee052012-11-29 14:15:57 +0200957 flags = SPS_IOVEC_FLAG_INT;
Joel Nider435ad8e2011-12-14 16:53:30 +0200958
959 /* start the transfer */
960 rc = sps_transfer_one(channel->pipe,
961 buffer->sps.phys_base,
962 buffer->sps.size,
963 channel->pdev,
964 flags);
965 if (rc < 0)
966 return rc;
967
968 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200969
970 return 0;
971}
972
973static int tspp_global_reset(struct tspp_device *pdev)
974{
975 u32 i, val;
976
977 /* stop all TSIFs */
978 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
979 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
980 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
981 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmany92705b32012-10-23 14:15:41 +0200982 pdev->tsif[i].clock_inverse = 0;
983 pdev->tsif[i].data_inverse = 0;
984 pdev->tsif[i].sync_inverse = 0;
985 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200986 }
987 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
988 wmb();
989
990 /* BAM */
991 if (sps_device_reset(pdev->bam_handle) != 0) {
992 pr_err("tspp: error resetting bam");
Joel Nider435ad8e2011-12-14 16:53:30 +0200993 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200994 }
995
996 /* TSPP tables */
997 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider435ad8e2011-12-14 16:53:30 +0200998 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +0200999 0, sizeof(struct tspp_pid_filter_table));
1000
1001 /* disable all filters */
1002 val = (2 << TSPP_NUM_CHANNELS) - 1;
1003 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
1004
1005 /* TSPP registers */
1006 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1007 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1008 pdev->base + TSPP_CONTROL);
1009 wmb();
Joel Nider435ad8e2011-12-14 16:53:30 +02001010 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001011 sizeof(struct tspp_global_performance_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001012 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001013 sizeof(struct tspp_pipe_context_regs));
Joel Nider435ad8e2011-12-14 16:53:30 +02001014 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +02001015 sizeof(struct tspp_pipe_performance_regs));
1016 wmb();
1017 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
1018 pdev->base + TSPP_CONTROL);
1019 wmb();
1020
1021 val = readl_relaxed(pdev->base + TSPP_CONFIG);
1022 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
1023 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
1024 TSPP_CONFIG_PS_CONT_ERR_MASK);
1025 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
1026 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany6bac7832012-12-20 18:30:40 +02001027 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +02001028 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
1029 writel_relaxed(0, pdev->base + TSPP_RST);
1030 wmb();
1031
1032 tspp_key_entry = 0;
1033
1034 return 0;
1035}
1036
Joel Nider435ad8e2011-12-14 16:53:30 +02001037static int tspp_select_source(u32 dev, u32 channel_id,
1038 struct tspp_select_source *src)
1039{
1040 /* make sure the requested src id is in bounds */
1041 if (src->source > TSPP_SOURCE_MEM) {
1042 pr_err("tspp source out of bounds");
1043 return -EINVAL;
1044 }
1045
1046 /* open the stream */
Hamad Kadmany92705b32012-10-23 14:15:41 +02001047 tspp_open_stream(dev, channel_id, src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001048
1049 return 0;
1050}
1051
1052static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1053{
1054 struct tspp_device *pdev = channel->pdev;
1055
1056 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1057 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1058 return 0;
1059}
1060
1061static int tspp_set_system_keys(struct tspp_channel *channel,
1062 struct tspp_system_keys *keys)
1063{
1064 int i;
1065 struct tspp_device *pdev = channel->pdev;
1066
1067 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1068 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1069
1070 return 0;
1071}
1072
1073static int tspp_channel_init(struct tspp_channel *channel,
1074 struct tspp_device *pdev)
1075{
1076 channel->cdev.owner = THIS_MODULE;
1077 cdev_init(&channel->cdev, &tspp_fops);
1078 channel->pdev = pdev;
1079 channel->data = NULL;
1080 channel->read = NULL;
1081 channel->waiting = NULL;
1082 channel->locked = NULL;
1083 channel->id = MINOR(tspp_minor);
1084 channel->used = 0;
1085 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1086 channel->max_buffers = TSPP_NUM_BUFFERS;
1087 channel->buffer_count = 0;
1088 channel->filter_count = 0;
1089 channel->int_freq = 1;
Liron Kuch72b78552012-10-30 17:47:50 +02001090 channel->src = TSPP_SOURCE_NONE;
1091 channel->mode = TSPP_MODE_DISABLED;
Joel Nider435ad8e2011-12-14 16:53:30 +02001092 channel->notifier = NULL;
1093 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001094 channel->expiration_period_ms = 0;
Liron Kuch72b78552012-10-30 17:47:50 +02001095 channel->memfree = NULL;
1096 channel->user_info = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001097 init_waitqueue_head(&channel->in_queue);
1098
1099 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1100 pr_err("tspp: cdev_add failed");
1101 return -EBUSY;
1102 }
1103
1104 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1105 channel, "tspp%02d", channel->id);
1106 if (IS_ERR(channel->dd)) {
1107 pr_err("tspp: device_create failed: %i",
1108 (int)PTR_ERR(channel->dd));
1109 cdev_del(&channel->cdev);
1110 return -EBUSY;
1111 }
1112
1113 return 0;
1114}
1115
1116static int tspp_set_buffer_size(struct tspp_channel *channel,
1117 struct tspp_buffer *buf)
1118{
Liron Kuch72b78552012-10-30 17:47:50 +02001119 if (channel->buffer_count > 0) {
1120 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1121 return -EPERM;
1122 }
1123
Joel Nider435ad8e2011-12-14 16:53:30 +02001124 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1125 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1126 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1127 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1128 else
1129 channel->buffer_size = buf->size;
1130
1131 return 0;
1132}
1133
1134static void tspp_set_tsif_mode(struct tspp_channel *channel,
1135 enum tspp_tsif_mode mode)
1136{
1137 int index;
1138
1139 switch (channel->src) {
1140 case TSPP_SOURCE_TSIF0:
1141 index = 0;
1142 break;
1143 case TSPP_SOURCE_TSIF1:
1144 index = 1;
1145 break;
1146 default:
1147 pr_warn("tspp: can't set mode for non-tsif source %d",
1148 channel->src);
1149 return;
1150 }
1151 channel->pdev->tsif[index].mode = mode;
1152}
1153
Hamad Kadmany92705b32012-10-23 14:15:41 +02001154static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch72b78552012-10-30 17:47:50 +02001155 int clock_inverse, int data_inverse,
1156 int sync_inverse, int enable_inverse)
Hamad Kadmany92705b32012-10-23 14:15:41 +02001157{
1158 int index;
1159
1160 switch (channel->src) {
1161 case TSPP_SOURCE_TSIF0:
1162 index = 0;
1163 break;
1164 case TSPP_SOURCE_TSIF1:
1165 index = 1;
1166 break;
1167 default:
1168 return;
1169 }
1170 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1171 channel->pdev->tsif[index].data_inverse = data_inverse;
1172 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1173 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1174}
1175
Liron Kuch72b78552012-10-30 17:47:50 +02001176static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1177{
1178 u32 alignment;
1179
1180 switch (mode) {
1181 case TSPP_MODE_RAW:
1182 /* must be a multiple of 192 */
1183 alignment = (TSPP_PACKET_LENGTH + 4);
1184 if (size % alignment)
1185 return 0;
1186 return 1;
1187
1188 case TSPP_MODE_RAW_NO_SUFFIX:
1189 /* must be a multiple of 188 */
1190 alignment = TSPP_PACKET_LENGTH;
1191 if (size % alignment)
1192 return 0;
1193 return 1;
1194
1195 case TSPP_MODE_DISABLED:
1196 case TSPP_MODE_PES:
1197 default:
1198 /* no alignment requirement */
1199 return 1;
1200 }
1201
1202}
1203
1204static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1205{
1206 u32 new_size;
1207 u32 alignment;
1208
1209 switch (mode) {
1210 case TSPP_MODE_RAW:
1211 /* must be a multiple of 192 */
1212 alignment = (TSPP_PACKET_LENGTH + 4);
1213 break;
1214
1215 case TSPP_MODE_RAW_NO_SUFFIX:
1216 /* must be a multiple of 188 */
1217 alignment = TSPP_PACKET_LENGTH;
1218 break;
1219
1220 case TSPP_MODE_DISABLED:
1221 case TSPP_MODE_PES:
1222 default:
1223 /* no alignment requirement - give the user what he asks for */
1224 alignment = 1;
1225 break;
1226 }
1227 /* align up */
1228 new_size = (((size + alignment - 1) / alignment) * alignment);
1229 return new_size;
1230}
1231
1232static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1233{
1234 int i;
1235 struct tspp_mem_buffer *pbuf, *temp;
1236
1237 pbuf = channel->data;
1238 for (i = 0; i < channel->buffer_count; i++) {
1239 if (pbuf->desc.phys_base) {
1240 if (channel->memfree) {
1241 channel->memfree(channel_id,
1242 pbuf->desc.size,
1243 pbuf->desc.virt_base,
1244 pbuf->desc.phys_base,
1245 channel->user_info);
1246 } else {
Hamad Kadmany090709b2013-01-06 12:08:13 +02001247 if (!channel->dma_pool)
1248 dma_free_coherent(NULL,
1249 pbuf->desc.size,
1250 pbuf->desc.virt_base,
1251 pbuf->desc.phys_base);
1252 else
1253 dma_pool_free(channel->dma_pool,
1254 pbuf->desc.virt_base,
1255 pbuf->desc.phys_base);
Liron Kuch72b78552012-10-30 17:47:50 +02001256 }
1257 pbuf->desc.phys_base = 0;
1258 }
1259 pbuf->desc.virt_base = 0;
1260 pbuf->state = TSPP_BUF_STATE_EMPTY;
1261 temp = pbuf;
1262 pbuf = pbuf->next;
1263 kfree(temp);
1264 }
1265}
1266
Joel Nider435ad8e2011-12-14 16:53:30 +02001267/*** TSPP API functions ***/
Liron Kuch72b78552012-10-30 17:47:50 +02001268
1269/**
1270 * tspp_open_stream - open a TSPP stream for use.
1271 *
1272 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1273 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1274 * @source: stream source parameters.
1275 *
1276 * Return error status
1277 *
1278 */
1279int tspp_open_stream(u32 dev, u32 channel_id,
1280 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001281{
1282 u32 val;
1283 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001284 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001285
Hamad Kadmany92705b32012-10-23 14:15:41 +02001286 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1287 dev, channel_id, source->source, source->mode);
Liron Kuch72b78552012-10-30 17:47:50 +02001288
Joel Nider435ad8e2011-12-14 16:53:30 +02001289 if (dev >= TSPP_MAX_DEVICES) {
1290 pr_err("tspp: device id out of range");
1291 return -ENODEV;
1292 }
Joel Nider5556a852011-10-16 10:52:13 +02001293
Joel Nider435ad8e2011-12-14 16:53:30 +02001294 if (channel_id >= TSPP_NUM_CHANNELS) {
1295 pr_err("tspp: channel id out of range");
1296 return -ECHRNG;
1297 }
1298
1299 pdev = tspp_find_by_id(dev);
1300 if (!pdev) {
1301 pr_err("tspp_str: can't find device %i", dev);
1302 return -ENODEV;
1303 }
1304 channel = &pdev->channels[channel_id];
Hamad Kadmany92705b32012-10-23 14:15:41 +02001305 channel->src = source->source;
1306 tspp_set_tsif_mode(channel, source->mode);
1307 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch72b78552012-10-30 17:47:50 +02001308 source->data_inverse, source->sync_inverse,
1309 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001310
Hamad Kadmany92705b32012-10-23 14:15:41 +02001311 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001312 case TSPP_SOURCE_TSIF0:
Liron Kuch275c0b32013-02-10 15:19:32 +02001313 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1314 pr_err("tspp: error enabling tsif0 GPIOs\n");
1315 return -EBUSY;
1316 }
Joel Nider5556a852011-10-16 10:52:13 +02001317 /* make sure TSIF0 is running & enabled */
1318 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1319 pr_err("tspp: error starting tsif0");
Joel Nider435ad8e2011-12-14 16:53:30 +02001320 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001321 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001322 if (pdev->tsif[0].ref_count == 1) {
1323 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1324 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1325 pdev->base + TSPP_CONTROL);
1326 wmb();
1327 }
Joel Nider5556a852011-10-16 10:52:13 +02001328 break;
1329 case TSPP_SOURCE_TSIF1:
Liron Kuch275c0b32013-02-10 15:19:32 +02001330 if (tspp_config_gpios(pdev, channel->src, 1) != 0) {
1331 pr_err("tspp: error enabling tsif1 GPIOs\n");
1332 return -EBUSY;
1333 }
Joel Nider5556a852011-10-16 10:52:13 +02001334 /* make sure TSIF1 is running & enabled */
1335 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1336 pr_err("tspp: error starting tsif1");
Joel Nider435ad8e2011-12-14 16:53:30 +02001337 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001338 }
Liron Kucha7b49ae2013-02-14 16:26:38 +02001339 if (pdev->tsif[1].ref_count == 1) {
1340 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1341 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1342 pdev->base + TSPP_CONTROL);
1343 wmb();
1344 }
Joel Nider5556a852011-10-16 10:52:13 +02001345 break;
1346 case TSPP_SOURCE_MEM:
1347 break;
1348 default:
Hamad Kadmany92705b32012-10-23 14:15:41 +02001349 pr_err("tspp: channel %i invalid source %i",
1350 channel->id, source->source);
Joel Nider435ad8e2011-12-14 16:53:30 +02001351 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001352 }
1353
Joel Nider5556a852011-10-16 10:52:13 +02001354 return 0;
1355}
1356EXPORT_SYMBOL(tspp_open_stream);
1357
Liron Kuch72b78552012-10-30 17:47:50 +02001358/**
1359 * tspp_close_stream - close a TSPP stream.
1360 *
1361 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1362 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1363 *
1364 * Return error status
1365 *
1366 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001367int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001368{
1369 u32 val;
Liron Kucha7b49ae2013-02-14 16:26:38 +02001370 u32 prev_ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001371 struct tspp_device *pdev;
Joel Nider435ad8e2011-12-14 16:53:30 +02001372 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001373
Joel Nider435ad8e2011-12-14 16:53:30 +02001374 if (channel_id >= TSPP_NUM_CHANNELS) {
1375 pr_err("tspp: channel id out of range");
1376 return -ECHRNG;
1377 }
1378 pdev = tspp_find_by_id(dev);
1379 if (!pdev) {
1380 pr_err("tspp_cs: can't find device %i", dev);
1381 return -EBUSY;
1382 }
1383 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001384
1385 switch (channel->src) {
1386 case TSPP_SOURCE_TSIF0:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001387 prev_ref_count = pdev->tsif[0].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001388 tspp_stop_tsif(&pdev->tsif[0]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001389 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1390 pr_err("tspp: error disabling tsif0 GPIOs\n");
1391
Liron Kucha7b49ae2013-02-14 16:26:38 +02001392 if (prev_ref_count == 1) {
1393 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1394 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1395 pdev->base + TSPP_CONTROL);
1396 wmb();
1397 }
Joel Nider5556a852011-10-16 10:52:13 +02001398 break;
1399 case TSPP_SOURCE_TSIF1:
Liron Kucha7b49ae2013-02-14 16:26:38 +02001400 prev_ref_count = pdev->tsif[1].ref_count;
Joel Nider5556a852011-10-16 10:52:13 +02001401 tspp_stop_tsif(&pdev->tsif[1]);
Liron Kuch275c0b32013-02-10 15:19:32 +02001402 if (tspp_config_gpios(pdev, channel->src, 0) != 0)
1403 pr_err("tspp: error disabling tsif0 GPIOs\n");
1404
Liron Kucha7b49ae2013-02-14 16:26:38 +02001405 if (prev_ref_count == 1) {
1406 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1407 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1408 pdev->base + TSPP_CONTROL);
1409 wmb();
1410 }
Joel Nider5556a852011-10-16 10:52:13 +02001411 break;
1412 case TSPP_SOURCE_MEM:
1413 break;
1414 case TSPP_SOURCE_NONE:
1415 break;
1416 }
1417
Joel Nider435ad8e2011-12-14 16:53:30 +02001418 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001419 return 0;
1420}
1421EXPORT_SYMBOL(tspp_close_stream);
1422
Liron Kuch72b78552012-10-30 17:47:50 +02001423/**
1424 * tspp_open_channel - open a TSPP channel.
1425 *
1426 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1427 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1428 *
1429 * Return error status
1430 *
1431 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001432int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001433{
1434 int rc = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001435 struct sps_connect *config;
1436 struct sps_register_event *event;
1437 struct tspp_channel *channel;
1438 struct tspp_device *pdev;
1439
1440 if (channel_id >= TSPP_NUM_CHANNELS) {
1441 pr_err("tspp: channel id out of range");
1442 return -ECHRNG;
1443 }
1444 pdev = tspp_find_by_id(dev);
1445 if (!pdev) {
1446 pr_err("tspp_oc: can't find device %i", dev);
1447 return -ENODEV;
1448 }
1449 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001450
1451 if (channel->used) {
1452 pr_err("tspp channel already in use");
Joel Nider435ad8e2011-12-14 16:53:30 +02001453 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001454 }
1455
Joel Nider435ad8e2011-12-14 16:53:30 +02001456 config = &channel->config;
1457 event = &channel->event;
1458
1459 /* start the clocks if needed */
Liron Kuch59339922013-01-01 18:29:47 +02001460 if (tspp_channels_in_use(pdev) == 0) {
1461 tspp_clock_start(pdev);
Joel Nider435ad8e2011-12-14 16:53:30 +02001462 wake_lock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001463 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001464
Joel Nider5556a852011-10-16 10:52:13 +02001465 /* mark it as used */
1466 channel->used = 1;
1467
1468 /* start the bam */
1469 channel->pipe = sps_alloc_endpoint();
1470 if (channel->pipe == 0) {
1471 pr_err("tspp: error allocating endpoint");
1472 rc = -ENOMEM;
1473 goto err_sps_alloc;
1474 }
1475
1476 /* get default configuration */
1477 sps_get_config(channel->pipe, config);
1478
Joel Nider435ad8e2011-12-14 16:53:30 +02001479 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001480 config->destination = SPS_DEV_HANDLE_MEM;
1481 config->mode = SPS_MODE_SRC;
Joel Nider435ad8e2011-12-14 16:53:30 +02001482 config->options =
1483 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1484 SPS_O_STREAMING | /* streaming mode */
1485 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany81cee052012-11-29 14:15:57 +02001486 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1487 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001488 config->src_pipe_index = channel->id;
1489 config->desc.size =
Hamad Kadmany81cee052012-11-29 14:15:57 +02001490 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001491 config->desc.base = dma_alloc_coherent(NULL,
1492 config->desc.size,
1493 &config->desc.phys_base,
1494 GFP_KERNEL);
1495 if (config->desc.base == 0) {
1496 pr_err("tspp: error allocating sps descriptors");
1497 rc = -ENOMEM;
1498 goto err_desc_alloc;
1499 }
1500
1501 memset(config->desc.base, 0, config->desc.size);
1502
1503 rc = sps_connect(channel->pipe, config);
1504 if (rc) {
1505 pr_err("tspp: error connecting bam");
1506 goto err_connect;
1507 }
1508
1509 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider435ad8e2011-12-14 16:53:30 +02001510 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001511 event->callback = tspp_sps_complete_cb;
1512 event->xfer_done = NULL;
Joel Nider435ad8e2011-12-14 16:53:30 +02001513 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001514
1515 rc = sps_register_event(channel->pipe, event);
1516 if (rc) {
1517 pr_err("tspp: error registering event");
1518 goto err_event;
1519 }
1520
Hamad Kadmany81cee052012-11-29 14:15:57 +02001521 init_timer(&channel->expiration_timer);
1522 channel->expiration_timer.function = tspp_expiration_timer;
1523 channel->expiration_timer.data = (unsigned long)pdev;
1524 channel->expiration_timer.expires = 0xffffffffL;
1525
Joel Nider435ad8e2011-12-14 16:53:30 +02001526 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001527 if (rc < 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001528 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001529 "Runtime PM: Unable to wake up tspp device, rc = %d",
1530 rc);
1531 }
Joel Nider5556a852011-10-16 10:52:13 +02001532 return 0;
1533
1534err_event:
1535 sps_disconnect(channel->pipe);
1536err_connect:
1537 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1538 config->desc.phys_base);
1539err_desc_alloc:
1540 sps_free_endpoint(channel->pipe);
1541err_sps_alloc:
1542 return rc;
1543}
1544EXPORT_SYMBOL(tspp_open_channel);
1545
Liron Kuch72b78552012-10-30 17:47:50 +02001546/**
1547 * tspp_close_channel - close a TSPP channel.
1548 *
1549 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1550 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1551 *
1552 * Return error status
1553 *
1554 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001555int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001556{
1557 int i;
1558 int id;
1559 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001560
Joel Nider435ad8e2011-12-14 16:53:30 +02001561 struct sps_connect *config;
1562 struct tspp_device *pdev;
1563 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02001564
1565 if (channel_id >= TSPP_NUM_CHANNELS) {
1566 pr_err("tspp: channel id out of range");
1567 return -ECHRNG;
1568 }
1569 pdev = tspp_find_by_id(dev);
1570 if (!pdev) {
1571 pr_err("tspp_close: can't find device %i", dev);
1572 return -ENODEV;
1573 }
1574 channel = &pdev->channels[channel_id];
1575
1576 /* if the channel is not used, we are done */
1577 if (!channel->used)
1578 return 0;
1579
Hamad Kadmany81cee052012-11-29 14:15:57 +02001580 if (channel->expiration_period_ms)
1581 del_timer(&channel->expiration_timer);
1582
Joel Nider435ad8e2011-12-14 16:53:30 +02001583 channel->notifier = NULL;
1584 channel->notify_data = NULL;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001585 channel->expiration_period_ms = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001586
1587 config = &channel->config;
1588 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001589
1590 /* disable pipe (channel) */
1591 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1592 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1593 wmb();
1594
1595 /* unregister all filters for this channel */
1596 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1597 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001598 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001599 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1600 if (id == channel->id) {
1601 if (FILTER_HAS_ENCRYPTION(tspp_filter))
1602 tspp_free_key_entry(
1603 FILTER_GET_KEY_NUMBER(tspp_filter));
1604 tspp_filter->config = 0;
1605 tspp_filter->filter = 0;
1606 }
1607 }
1608 channel->filter_count = 0;
1609
Joel Nider5556a852011-10-16 10:52:13 +02001610 /* disconnect the bam */
1611 if (sps_disconnect(channel->pipe) != 0)
1612 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1613
1614 /* destroy the buffers */
1615 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1616 config->desc.phys_base);
1617
Liron Kuch72b78552012-10-30 17:47:50 +02001618 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmany090709b2013-01-06 12:08:13 +02001619 if (channel->dma_pool) {
1620 dma_pool_destroy(channel->dma_pool);
1621 channel->dma_pool = NULL;
1622 }
Liron Kuch72b78552012-10-30 17:47:50 +02001623
1624 channel->src = TSPP_SOURCE_NONE;
1625 channel->mode = TSPP_MODE_DISABLED;
1626 channel->memfree = NULL;
1627 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001628 channel->buffer_count = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001629 channel->data = NULL;
1630 channel->read = NULL;
1631 channel->waiting = NULL;
1632 channel->locked = NULL;
1633 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001634
Liron Kuch59339922013-01-01 18:29:47 +02001635 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001636 wake_unlock(&pdev->wake_lock);
Liron Kuch59339922013-01-01 18:29:47 +02001637 tspp_clock_stop(pdev);
1638 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001639
Joel Nider5556a852011-10-16 10:52:13 +02001640 return 0;
1641}
1642EXPORT_SYMBOL(tspp_close_channel);
1643
Liron Kuch72b78552012-10-30 17:47:50 +02001644/**
Hamad Kadmany6d2a9c72013-01-31 14:49:20 +02001645 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1646 *
1647 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1648 * @source: The TSIF source from which the counter should be read
1649 * @tcr_counter: the value of TCR counter
1650 *
1651 * Return error status
1652 *
1653 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1654 * If source is neither TSIF 0 or TSIF1 0 is returned.
1655 */
1656int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1657{
1658 struct tspp_device *pdev;
1659 struct tspp_tsif_device *tsif_device;
1660
1661 if (!tcr_counter)
1662 return -EINVAL;
1663
1664 pdev = tspp_find_by_id(dev);
1665 if (!pdev) {
1666 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1667 return -ENODEV;
1668 }
1669
1670 switch (source) {
1671 case TSPP_SOURCE_TSIF0:
1672 tsif_device = &pdev->tsif[0];
1673 break;
1674
1675 case TSPP_SOURCE_TSIF1:
1676 tsif_device = &pdev->tsif[1];
1677 break;
1678
1679 default:
1680 tsif_device = NULL;
1681 break;
1682 }
1683
1684 if (tsif_device && tsif_device->ref_count)
1685 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1686 else
1687 *tcr_counter = 0;
1688
1689 return 0;
1690}
1691EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1692
1693/**
Liron Kuch72b78552012-10-30 17:47:50 +02001694 * tspp_add_filter - add a TSPP filter to a channel.
1695 *
1696 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1697 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1698 * @filter: TSPP filter parameters
1699 *
1700 * Return error status
1701 *
1702 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001703int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001704 struct tspp_filter *filter)
1705{
Liron Kuch72b78552012-10-30 17:47:50 +02001706 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001707 int other_channel;
1708 int entry;
1709 u32 val, pid, enabled;
Joel Nider435ad8e2011-12-14 16:53:30 +02001710 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001711 struct tspp_pid_filter p;
Joel Nider435ad8e2011-12-14 16:53:30 +02001712 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001713
Joel Nider435ad8e2011-12-14 16:53:30 +02001714 TSPP_DEBUG("tspp: add filter");
1715 if (channel_id >= TSPP_NUM_CHANNELS) {
1716 pr_err("tspp: channel id out of range");
1717 return -ECHRNG;
1718 }
1719 pdev = tspp_find_by_id(dev);
1720 if (!pdev) {
1721 pr_err("tspp_add: can't find device %i", dev);
1722 return -ENODEV;
1723 }
1724
1725 channel = &pdev->channels[channel_id];
1726
Joel Nider5556a852011-10-16 10:52:13 +02001727 if (filter->source > TSPP_SOURCE_MEM) {
1728 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001729 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001730 }
1731
1732 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1733 pr_err("tspp invalid source");
Joel Nider435ad8e2011-12-14 16:53:30 +02001734 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001735 }
1736
Liron Kuch72b78552012-10-30 17:47:50 +02001737 channel->mode = filter->mode;
1738 /*
1739 * if buffers are already allocated, verify they fulfil
1740 * the alignment requirements.
1741 */
1742 if ((channel->buffer_count > 0) &&
1743 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1744 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001745
1746 if (filter->mode == TSPP_MODE_PES) {
1747 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1748 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001749 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001750 pid = FILTER_GET_PIPE_PID((tspp_filter));
1751 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1752 if (enabled && (pid == filter->pid)) {
1753 other_channel =
1754 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1755 pr_err("tspp: pid 0x%x already in use by channel %i",
1756 filter->pid, other_channel);
Joel Nider435ad8e2011-12-14 16:53:30 +02001757 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001758 }
1759 }
1760 }
1761
1762 /* make sure this priority is not already in use */
1763 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider435ad8e2011-12-14 16:53:30 +02001764 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001765 if (enabled) {
1766 pr_err("tspp: filter priority %i source %i is already enabled\n",
1767 filter->priority, channel->src);
Joel Nider435ad8e2011-12-14 16:53:30 +02001768 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001769 }
1770
1771 if (channel->mode == TSPP_MODE_PES) {
1772 /* if we are already processing in PES mode, disable pipe
1773 (channel) and filter to be updated */
1774 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1775 writel_relaxed(val | (1 << channel->id),
1776 pdev->base + TSPP_PS_DISABLE);
1777 wmb();
1778 }
1779
1780 /* update entry */
1781 p.filter = 0;
Joel Nider435ad8e2011-12-14 16:53:30 +02001782 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001783 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1784 FILTER_SET_PIPE_PID((&p), filter->pid);
1785 FILTER_SET_PID_MASK((&p), filter->mask);
1786 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1787 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1788 if (filter->decrypt) {
1789 entry = tspp_get_key_entry();
1790 if (entry == -1) {
1791 pr_err("tspp: no more keys available!");
1792 } else {
1793 p.config |= FILTER_DECRYPT;
1794 FILTER_SET_KEY_NUMBER((&p), entry);
1795 }
1796 }
Joel Nider5556a852011-10-16 10:52:13 +02001797
Joel Nider435ad8e2011-12-14 16:53:30 +02001798 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001799 filter[filter->priority].config = p.config;
Joel Nider435ad8e2011-12-14 16:53:30 +02001800 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001801 filter[filter->priority].filter = p.filter;
1802
Liron Kuch72b78552012-10-30 17:47:50 +02001803 /*
1804 * allocate buffers if needed (i.e. if user did has not already called
1805 * tspp_allocate_buffers() explicitly).
1806 */
1807 if (channel->buffer_count == 0) {
1808 channel->buffer_size =
Hamad Kadmany090709b2013-01-06 12:08:13 +02001809 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch72b78552012-10-30 17:47:50 +02001810 channel->mode);
1811 rc = tspp_allocate_buffers(dev, channel->id,
1812 channel->max_buffers,
1813 channel->buffer_size,
1814 channel->int_freq, NULL, NULL, NULL);
1815 if (rc != 0) {
1816 pr_err("tspp: tspp_allocate_buffers failed\n");
1817 return rc;
1818 }
Joel Nider435ad8e2011-12-14 16:53:30 +02001819 }
1820
Joel Nider5556a852011-10-16 10:52:13 +02001821 /* reenable pipe */
1822 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1823 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1824 wmb();
1825 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1826
Joel Nider5556a852011-10-16 10:52:13 +02001827 channel->filter_count++;
1828
1829 return 0;
1830}
1831EXPORT_SYMBOL(tspp_add_filter);
1832
Liron Kuch72b78552012-10-30 17:47:50 +02001833/**
1834 * tspp_remove_filter - remove a TSPP filter from a channel.
1835 *
1836 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1837 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1838 * @filter: TSPP filter parameters
1839 *
1840 * Return error status
1841 *
1842 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001843int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001844 struct tspp_filter *filter)
1845{
1846 int entry;
1847 u32 val;
Joel Nider435ad8e2011-12-14 16:53:30 +02001848 struct tspp_device *pdev;
1849 int src;
1850 struct tspp_pid_filter *tspp_filter;
1851 struct tspp_channel *channel;
1852
1853 if (channel_id >= TSPP_NUM_CHANNELS) {
1854 pr_err("tspp: channel id out of range");
1855 return -ECHRNG;
1856 }
1857 pdev = tspp_find_by_id(dev);
1858 if (!pdev) {
1859 pr_err("tspp_remove: can't find device %i", dev);
1860 return -ENODEV;
1861 }
1862 channel = &pdev->channels[channel_id];
1863
1864 src = channel->src;
1865 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001866
1867 /* disable pipe (channel) */
1868 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1869 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1870 wmb();
1871
1872 /* update data keys */
1873 if (tspp_filter->config & FILTER_DECRYPT) {
1874 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1875 tspp_free_key_entry(entry);
1876 }
1877
1878 /* update pid table */
1879 tspp_filter->config = 0;
1880 tspp_filter->filter = 0;
1881
1882 channel->filter_count--;
1883
1884 /* reenable pipe */
1885 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1886 writel_relaxed(val & ~(1 << channel->id),
1887 pdev->base + TSPP_PS_DISABLE);
1888 wmb();
1889 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1890
1891 return 0;
1892}
1893EXPORT_SYMBOL(tspp_remove_filter);
1894
Liron Kuch72b78552012-10-30 17:47:50 +02001895/**
1896 * tspp_set_key - set TSPP key in key table.
1897 *
1898 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1899 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1900 * @key: TSPP key parameters
1901 *
1902 * Return error status
1903 *
1904 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001905int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001906{
1907 int i;
1908 int id;
1909 int key_index;
1910 int data;
Joel Nider435ad8e2011-12-14 16:53:30 +02001911 struct tspp_channel *channel;
1912 struct tspp_device *pdev;
1913
1914 if (channel_id >= TSPP_NUM_CHANNELS) {
1915 pr_err("tspp: channel id out of range");
1916 return -ECHRNG;
1917 }
1918 pdev = tspp_find_by_id(dev);
1919 if (!pdev) {
1920 pr_err("tspp_set: can't find device %i", dev);
1921 return -ENODEV;
1922 }
1923 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001924
1925 /* read the key index used by this channel */
1926 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1927 struct tspp_pid_filter *tspp_filter =
Joel Nider435ad8e2011-12-14 16:53:30 +02001928 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001929 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1930 if (id == channel->id) {
1931 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1932 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1933 break;
1934 }
1935 }
1936 }
1937 if (i == TSPP_NUM_PRIORITIES) {
1938 pr_err("tspp: no encryption on this channel");
Joel Nider435ad8e2011-12-14 16:53:30 +02001939 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001940 }
1941
1942 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider435ad8e2011-12-14 16:53:30 +02001943 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1944 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001945 } else {
Joel Nider435ad8e2011-12-14 16:53:30 +02001946 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1947 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001948 }
1949 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1950
1951 return 0;
1952}
1953EXPORT_SYMBOL(tspp_set_key);
1954
Liron Kuch72b78552012-10-30 17:47:50 +02001955/**
1956 * tspp_register_notification - register TSPP channel notification function.
1957 *
1958 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1959 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1960 * @pNotify: notification function
1961 * @userdata: user data to pass to notification function
1962 * @timer_ms: notification for partially filled buffers
1963 *
1964 * Return error status
1965 *
1966 */
Joel Nider435ad8e2011-12-14 16:53:30 +02001967int tspp_register_notification(u32 dev, u32 channel_id,
1968 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001969{
Joel Nider435ad8e2011-12-14 16:53:30 +02001970 struct tspp_channel *channel;
1971 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001972
Joel Nider435ad8e2011-12-14 16:53:30 +02001973 if (channel_id >= TSPP_NUM_CHANNELS) {
1974 pr_err("tspp: channel id out of range");
1975 return -ECHRNG;
1976 }
1977 pdev = tspp_find_by_id(dev);
1978 if (!pdev) {
1979 pr_err("tspp_reg: can't find device %i", dev);
1980 return -ENODEV;
1981 }
1982 channel = &pdev->channels[channel_id];
1983 channel->notifier = pNotify;
1984 channel->notify_data = userdata;
Hamad Kadmany81cee052012-11-29 14:15:57 +02001985 channel->expiration_period_ms = timer_ms;
1986
Joel Nider5556a852011-10-16 10:52:13 +02001987 return 0;
1988}
Joel Nider435ad8e2011-12-14 16:53:30 +02001989EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001990
Liron Kuch72b78552012-10-30 17:47:50 +02001991/**
1992 * tspp_unregister_notification - unregister TSPP channel notification function.
1993 *
1994 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1995 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1996 *
1997 * Return error status
1998 *
1999 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002000int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002001{
Joel Nider435ad8e2011-12-14 16:53:30 +02002002 struct tspp_channel *channel;
2003 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002004
Joel Nider435ad8e2011-12-14 16:53:30 +02002005 if (channel_id >= TSPP_NUM_CHANNELS) {
2006 pr_err("tspp: channel id out of range");
2007 return -ECHRNG;
2008 }
2009 pdev = tspp_find_by_id(dev);
2010 if (!pdev) {
2011 pr_err("tspp_unreg: can't find device %i", dev);
2012 return -ENODEV;
2013 }
2014 channel = &pdev->channels[channel_id];
2015 channel->notifier = NULL;
2016 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02002017 return 0;
2018}
Joel Nider435ad8e2011-12-14 16:53:30 +02002019EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02002020
Liron Kuch72b78552012-10-30 17:47:50 +02002021/**
2022 * tspp_get_buffer - get TSPP data buffer.
2023 *
2024 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2025 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2026 *
2027 * Return error status
2028 *
2029 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002030const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02002031{
Joel Nider435ad8e2011-12-14 16:53:30 +02002032 struct tspp_mem_buffer *buffer;
2033 struct tspp_channel *channel;
2034 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02002035
Joel Nider435ad8e2011-12-14 16:53:30 +02002036 if (channel_id >= TSPP_NUM_CHANNELS) {
2037 pr_err("tspp: channel id out of range");
2038 return NULL;
2039 }
2040 pdev = tspp_find_by_id(dev);
2041 if (!pdev) {
2042 pr_err("tspp_get: can't find device %i", dev);
2043 return NULL;
2044 }
2045 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02002046
Joel Nider435ad8e2011-12-14 16:53:30 +02002047 if (!channel->read) {
2048 pr_warn("tspp: no buffer to get on channel %i!",
2049 channel->id);
2050 return NULL;
2051 }
2052
2053 buffer = channel->read;
2054 /* see if we have any buffers ready to read */
2055 if (buffer->state != TSPP_BUF_STATE_DATA)
2056 return 0;
2057
2058 if (buffer->state == TSPP_BUF_STATE_DATA) {
2059 /* mark the buffer as busy */
2060 buffer->state = TSPP_BUF_STATE_LOCKED;
2061
2062 /* increment the pointer along the list */
2063 channel->read = channel->read->next;
2064 }
2065
2066 return &buffer->desc;
2067}
2068EXPORT_SYMBOL(tspp_get_buffer);
2069
Liron Kuch72b78552012-10-30 17:47:50 +02002070/**
2071 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2072 *
2073 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2074 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2075 * @descriptor_id: buffer descriptor ID
2076 *
2077 * Return error status
2078 *
2079 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002080int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2081{
2082 int i, found = 0;
2083 struct tspp_mem_buffer *buffer;
2084 struct tspp_channel *channel;
2085 struct tspp_device *pdev;
2086
2087 if (channel_id >= TSPP_NUM_CHANNELS) {
2088 pr_err("tspp: channel id out of range");
2089 return -ECHRNG;
2090 }
2091 pdev = tspp_find_by_id(dev);
2092 if (!pdev) {
2093 pr_err("tspp: can't find device %i", dev);
2094 return -ENODEV;
2095 }
2096 channel = &pdev->channels[channel_id];
2097
2098 if (descriptor_id > channel->buffer_count)
2099 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2100
2101 /* find the correct descriptor */
2102 buffer = channel->locked;
2103 for (i = 0; i < channel->buffer_count; i++) {
2104 if (buffer->desc.id == descriptor_id) {
2105 found = 1;
2106 break;
2107 }
2108 buffer = buffer->next;
2109 }
2110 channel->locked = channel->locked->next;
2111
2112 if (!found) {
2113 pr_err("tspp: cant find desc %i", descriptor_id);
2114 return -EINVAL;
2115 }
2116
2117 /* make sure the buffer is in the expected state */
2118 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2119 pr_err("tspp: buffer %i not locked", descriptor_id);
2120 return -EINVAL;
2121 }
2122 /* unlock the buffer and requeue it */
2123 buffer->state = TSPP_BUF_STATE_WAITING;
2124
2125 if (tspp_queue_buffer(channel, buffer))
2126 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002127 return 0;
2128}
Joel Nider435ad8e2011-12-14 16:53:30 +02002129EXPORT_SYMBOL(tspp_release_buffer);
2130
Liron Kuch72b78552012-10-30 17:47:50 +02002131/**
2132 * tspp_allocate_buffers - allocate TSPP data buffers.
2133 *
2134 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2135 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2136 * @count: number of buffers to allocate
2137 * @size: size of each buffer to allocate
2138 * @int_freq: interrupt frequency
2139 * @alloc: user defined memory allocator function. Pass NULL for default.
2140 * @memfree: user defined memory free function. Pass NULL for default.
2141 * @user: user data to pass to the memory allocator/free function
2142 *
2143 * Return error status
2144 *
2145 * The user can optionally call this function explicitly to allocate the TSPP
2146 * data buffers. Alternatively, if the user did not call this function, it
2147 * is called implicitly by tspp_add_filter().
2148 */
2149int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2150 u32 int_freq, tspp_allocator *alloc,
2151 tspp_memfree *memfree, void *user)
Joel Nider435ad8e2011-12-14 16:53:30 +02002152{
2153 struct tspp_channel *channel;
2154 struct tspp_device *pdev;
2155 struct tspp_mem_buffer *last = NULL;
2156
2157 TSPP_DEBUG("tspp_allocate_buffers");
2158
2159 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch72b78552012-10-30 17:47:50 +02002160 pr_err("%s: channel id out of range", __func__);
Joel Nider435ad8e2011-12-14 16:53:30 +02002161 return -ECHRNG;
2162 }
Liron Kuch72b78552012-10-30 17:47:50 +02002163
Joel Nider435ad8e2011-12-14 16:53:30 +02002164 pdev = tspp_find_by_id(dev);
2165 if (!pdev) {
Liron Kuch72b78552012-10-30 17:47:50 +02002166 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider435ad8e2011-12-14 16:53:30 +02002167 return -ENODEV;
2168 }
Liron Kuch72b78552012-10-30 17:47:50 +02002169
2170 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2171 pr_err("%s: tspp requires a minimum of %i buffers\n",
2172 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2173 return -EINVAL;
2174 }
2175
Joel Nider435ad8e2011-12-14 16:53:30 +02002176 channel = &pdev->channels[channel_id];
Hamad Kadmany090709b2013-01-06 12:08:13 +02002177
Liron Kuch72b78552012-10-30 17:47:50 +02002178 /* allow buffer allocation only if there was no previous buffer
2179 * allocation for this channel.
2180 */
2181 if (channel->buffer_count > 0) {
2182 pr_err("%s: buffers already allocated for channel %u",
2183 __func__, channel_id);
2184 return -EINVAL;
2185 }
Joel Nider435ad8e2011-12-14 16:53:30 +02002186
2187 channel->max_buffers = count;
2188
2189 /* set up interrupt frequency */
Liron Kuch72b78552012-10-30 17:47:50 +02002190 if (int_freq > channel->max_buffers) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002191 int_freq = channel->max_buffers;
Liron Kuch72b78552012-10-30 17:47:50 +02002192 pr_warn("%s: setting interrupt frequency to %u\n",
2193 __func__, int_freq);
Joel Nider435ad8e2011-12-14 16:53:30 +02002194 }
Liron Kuch72b78552012-10-30 17:47:50 +02002195 channel->int_freq = int_freq;
2196 /*
2197 * it is the responsibility of the caller to tspp_allocate_buffers(),
2198 * whether it's the user or the driver, to make sure the size parameter
2199 * is compatible to the channel mode.
2200 */
2201 channel->buffer_size = size;
Joel Nider435ad8e2011-12-14 16:53:30 +02002202
Liron Kuch72b78552012-10-30 17:47:50 +02002203 /* save user defined memory free function for later use */
2204 channel->memfree = memfree;
2205 channel->user_info = user;
2206
Hamad Kadmany090709b2013-01-06 12:08:13 +02002207 /*
2208 * For small buffers, create a DMA pool so that memory
2209 * is not wasted through dma_alloc_coherent.
2210 */
2211 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2212 channel->dma_pool = dma_pool_create("tspp",
2213 NULL, channel->buffer_size, 0, 0);
2214 if (!channel->dma_pool) {
2215 pr_err("%s: Can't allocate memory pool\n", __func__);
2216 return -ENOMEM;
2217 }
2218 } else {
2219 channel->dma_pool = NULL;
2220 }
2221
2222
Liron Kuch72b78552012-10-30 17:47:50 +02002223 for (channel->buffer_count = 0;
2224 channel->buffer_count < channel->max_buffers;
Joel Nider435ad8e2011-12-14 16:53:30 +02002225 channel->buffer_count++) {
2226
2227 /* allocate the descriptor */
2228 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2229 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2230 if (!desc) {
Liron Kuch72b78552012-10-30 17:47:50 +02002231 pr_warn("%s: Can't allocate desc %i",
2232 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002233 break;
2234 }
2235
2236 desc->desc.id = channel->buffer_count;
2237 /* allocate the buffer */
2238 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmany090709b2013-01-06 12:08:13 +02002239 channel->buffer_size, channel->dma_pool,
2240 alloc, user) != 0) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002241 kfree(desc);
Liron Kuch72b78552012-10-30 17:47:50 +02002242 pr_warn("%s: Can't allocate buffer %i",
2243 __func__, channel->buffer_count);
Joel Nider435ad8e2011-12-14 16:53:30 +02002244 break;
2245 }
2246
2247 /* add the descriptor to the list */
2248 desc->filled = 0;
2249 desc->read_index = 0;
2250 if (!channel->data) {
2251 channel->data = desc;
2252 desc->next = channel->data;
2253 } else {
2254 last->next = desc;
2255 }
2256 last = desc;
2257 desc->next = channel->data;
2258
2259 /* prepare the sps descriptor */
2260 desc->sps.phys_base = desc->desc.phys_base;
2261 desc->sps.base = desc->desc.virt_base;
2262 desc->sps.size = desc->desc.size;
2263
2264 /* start the transfer */
2265 if (tspp_queue_buffer(channel, desc))
Liron Kuch72b78552012-10-30 17:47:50 +02002266 pr_err("%s: can't queue buffer %i",
2267 __func__, desc->desc.id);
2268 }
2269
2270 if (channel->buffer_count < channel->max_buffers) {
2271 /*
2272 * we failed to allocate the requested number of buffers.
2273 * we don't allow a partial success, so need to clean up here.
2274 */
2275 tspp_destroy_buffers(channel_id, channel);
2276 channel->buffer_count = 0;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002277
2278 if (channel->dma_pool) {
2279 dma_pool_destroy(channel->dma_pool);
2280 channel->dma_pool = NULL;
2281 }
Liron Kuch72b78552012-10-30 17:47:50 +02002282 return -ENOMEM;
Joel Nider435ad8e2011-12-14 16:53:30 +02002283 }
2284
2285 channel->waiting = channel->data;
2286 channel->read = channel->data;
2287 channel->locked = channel->data;
Liron Kuch72b78552012-10-30 17:47:50 +02002288
Hamad Kadmany81cee052012-11-29 14:15:57 +02002289 /* Now that buffers are scheduled to HW, kick data expiration timer */
2290 if (channel->expiration_period_ms)
2291 mod_timer(&channel->expiration_timer,
2292 jiffies +
2293 MSEC_TO_JIFFIES(
2294 channel->expiration_period_ms));
2295
Joel Nider435ad8e2011-12-14 16:53:30 +02002296 return 0;
2297}
2298EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002299
2300/*** File Operations ***/
2301static ssize_t tspp_open(struct inode *inode, struct file *filp)
2302{
Joel Nider435ad8e2011-12-14 16:53:30 +02002303 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002304 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002305
2306 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002307 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2308 filp->private_data = channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002309 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002310
2311 /* if this channel is already in use, quit */
2312 if (channel->used) {
2313 pr_err("tspp channel %i already in use",
2314 MINOR(channel->cdev.dev));
2315 return -EACCES;
2316 }
2317
Joel Nider435ad8e2011-12-14 16:53:30 +02002318 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002319 pr_err("tspp: error opening channel");
2320 return -EACCES;
2321 }
2322
2323 return 0;
2324}
2325
2326static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2327{
2328 unsigned long flags;
2329 unsigned int mask = 0;
2330 struct tspp_channel *channel;
2331 channel = filp->private_data;
2332
2333 /* register the wait queue for this channel */
2334 poll_wait(filp, &channel->in_queue, p);
2335
2336 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider435ad8e2011-12-14 16:53:30 +02002337 if (channel->read &&
2338 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002339 mask = POLLIN | POLLRDNORM;
2340
2341 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2342
2343 return mask;
2344}
2345
2346static ssize_t tspp_release(struct inode *inode, struct file *filp)
2347{
Joel Nider435ad8e2011-12-14 16:53:30 +02002348 struct tspp_channel *channel = filp->private_data;
2349 u32 dev = channel->pdev->pdev->id;
2350 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002351
Joel Nider435ad8e2011-12-14 16:53:30 +02002352 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002353
2354 return 0;
2355}
2356
2357static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2358 loff_t *f_pos)
2359{
2360 size_t size = 0;
2361 size_t transferred = 0;
2362 struct tspp_channel *channel;
2363 struct tspp_mem_buffer *buffer;
2364 channel = filp->private_data;
2365
2366 TSPP_DEBUG("tspp_read");
Joel Nider435ad8e2011-12-14 16:53:30 +02002367
2368 while (!channel->read) {
2369 if (filp->f_flags & O_NONBLOCK) {
2370 pr_warn("tspp: no buffer on channel %i!",
2371 channel->id);
2372 return -EAGAIN;
2373 }
2374 /* go to sleep if there is nothing to read */
2375 if (wait_event_interruptible(channel->in_queue,
2376 (channel->read != NULL))) {
2377 pr_err("tspp: rude awakening\n");
2378 return -ERESTARTSYS;
2379 }
2380 }
2381
2382 buffer = channel->read;
2383
Joel Nider5556a852011-10-16 10:52:13 +02002384 /* see if we have any buffers ready to read */
2385 while (buffer->state != TSPP_BUF_STATE_DATA) {
2386 if (filp->f_flags & O_NONBLOCK) {
2387 pr_warn("tspp: nothing to read on channel %i!",
2388 channel->id);
2389 return -EAGAIN;
2390 }
2391 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002392 if (wait_event_interruptible(channel->in_queue,
2393 (buffer->state == TSPP_BUF_STATE_DATA))) {
2394 pr_err("tspp: rude awakening\n");
2395 return -ERESTARTSYS;
2396 }
2397 }
2398
2399 while (buffer->state == TSPP_BUF_STATE_DATA) {
2400 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002401 if (size == 0)
2402 break;
2403
Joel Nider435ad8e2011-12-14 16:53:30 +02002404 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002405 buffer->read_index, size)) {
2406 pr_err("tspp: error copying to user buffer");
Joel Nider435ad8e2011-12-14 16:53:30 +02002407 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002408 }
2409 buf += size;
2410 count -= size;
2411 transferred += size;
2412 buffer->read_index += size;
2413
Liron Kuch72b78552012-10-30 17:47:50 +02002414 /*
2415 * after reading the end of the buffer, requeue it,
2416 * and set up for reading the next one
2417 */
Joel Nider435ad8e2011-12-14 16:53:30 +02002418 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002419 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmany090709b2013-01-06 12:08:13 +02002420
Joel Nider435ad8e2011-12-14 16:53:30 +02002421 if (tspp_queue_buffer(channel, buffer))
2422 pr_err("tspp: can't submit transfer");
Hamad Kadmany090709b2013-01-06 12:08:13 +02002423
Joel Nider435ad8e2011-12-14 16:53:30 +02002424 channel->locked = channel->read;
2425 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002426 }
2427 }
2428
2429 return transferred;
2430}
2431
2432static long tspp_ioctl(struct file *filp,
2433 unsigned int param0, unsigned long param1)
2434{
Joel Nider435ad8e2011-12-14 16:53:30 +02002435 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002436 int rc = -1;
2437 struct tspp_channel *channel;
Joel Nider435ad8e2011-12-14 16:53:30 +02002438 struct tspp_select_source ss;
2439 struct tspp_filter f;
2440 struct tspp_key k;
2441 struct tspp_iv iv;
2442 struct tspp_system_keys sk;
2443 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002444 channel = filp->private_data;
Joel Nider435ad8e2011-12-14 16:53:30 +02002445 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002446
Liron Kucha7b49ae2013-02-14 16:26:38 +02002447 if ((param0 != TSPP_IOCTL_CLOSE_STREAM) && !param1)
Joel Nider5556a852011-10-16 10:52:13 +02002448 return -EINVAL;
2449
2450 switch (param0) {
2451 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002452 if (!access_ok(VERIFY_READ, param1,
2453 sizeof(struct tspp_select_source))) {
2454 return -EBUSY;
2455 }
2456 if (__copy_from_user(&ss, (void *)param1,
2457 sizeof(struct tspp_select_source)) == 0)
2458 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002459 break;
2460 case TSPP_IOCTL_ADD_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002461 if (!access_ok(VERIFY_READ, param1,
2462 sizeof(struct tspp_filter))) {
2463 return -ENOSR;
2464 }
2465 if (__copy_from_user(&f, (void *)param1,
2466 sizeof(struct tspp_filter)) == 0)
2467 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002468 break;
2469 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider435ad8e2011-12-14 16:53:30 +02002470 if (!access_ok(VERIFY_READ, param1,
2471 sizeof(struct tspp_filter))) {
2472 return -EBUSY;
2473 }
2474 if (__copy_from_user(&f, (void *)param1,
2475 sizeof(struct tspp_filter)) == 0)
2476 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002477 break;
2478 case TSPP_IOCTL_SET_KEY:
Joel Nider435ad8e2011-12-14 16:53:30 +02002479 if (!access_ok(VERIFY_READ, param1,
2480 sizeof(struct tspp_key))) {
2481 return -EBUSY;
2482 }
2483 if (__copy_from_user(&k, (void *)param1,
2484 sizeof(struct tspp_key)) == 0)
2485 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002486 break;
2487 case TSPP_IOCTL_SET_IV:
Joel Nider435ad8e2011-12-14 16:53:30 +02002488 if (!access_ok(VERIFY_READ, param1,
2489 sizeof(struct tspp_iv))) {
2490 return -EBUSY;
2491 }
2492 if (__copy_from_user(&iv, (void *)param1,
2493 sizeof(struct tspp_iv)) == 0)
2494 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002495 break;
2496 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider435ad8e2011-12-14 16:53:30 +02002497 if (!access_ok(VERIFY_READ, param1,
2498 sizeof(struct tspp_system_keys))) {
2499 return -EINVAL;
2500 }
2501 if (__copy_from_user(&sk, (void *)param1,
2502 sizeof(struct tspp_system_keys)) == 0)
2503 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002504 break;
2505 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider435ad8e2011-12-14 16:53:30 +02002506 if (!access_ok(VERIFY_READ, param1,
2507 sizeof(struct tspp_buffer))) {
2508 rc = -EINVAL;
2509 }
2510 if (__copy_from_user(&b, (void *)param1,
2511 sizeof(struct tspp_buffer)) == 0)
2512 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002513 break;
Liron Kucha7b49ae2013-02-14 16:26:38 +02002514 case TSPP_IOCTL_CLOSE_STREAM:
2515 rc = tspp_close_stream(dev, channel->id);
2516 break;
Joel Nider5556a852011-10-16 10:52:13 +02002517 default:
2518 pr_err("tspp: Unknown ioctl %i", param0);
2519 }
2520
Liron Kuch72b78552012-10-30 17:47:50 +02002521 /*
2522 * normalize the return code in case one of the subfunctions does
2523 * something weird
2524 */
Joel Nider5556a852011-10-16 10:52:13 +02002525 if (rc != 0)
Joel Nider435ad8e2011-12-14 16:53:30 +02002526 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002527
2528 return rc;
2529}
2530
2531/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002532static int debugfs_iomem_x32_set(void *data, u64 val)
2533{
2534 writel_relaxed(val, data);
2535 wmb();
2536 return 0;
2537}
2538
2539static int debugfs_iomem_x32_get(void *data, u64 *val)
2540{
2541 *val = readl_relaxed(data);
2542 return 0;
2543}
2544
2545DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2546 debugfs_iomem_x32_set, "0x%08llx");
2547
2548static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2549 int instance)
2550{
2551 char name[10];
2552 snprintf(name, 10, "tsif%i", instance);
2553 tsif_device->dent_tsif = debugfs_create_dir(
2554 name, NULL);
2555 if (tsif_device->dent_tsif) {
2556 int i;
2557 void __iomem *base = tsif_device->base;
2558 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2559 tsif_device->debugfs_tsif_regs[i] =
2560 debugfs_create_file(
2561 debugfs_tsif_regs[i].name,
2562 debugfs_tsif_regs[i].mode,
2563 tsif_device->dent_tsif,
2564 base + debugfs_tsif_regs[i].offset,
2565 &fops_iomem_x32);
2566 }
Hamad Kadmany44307d32012-11-25 09:49:51 +02002567
2568 debugfs_create_u32(
2569 "stat_rx_chunks",
2570 S_IRUGO|S_IWUGO,
2571 tsif_device->dent_tsif,
2572 &tsif_device->stat_rx);
2573
2574 debugfs_create_u32(
2575 "stat_overflow",
2576 S_IRUGO|S_IWUGO,
2577 tsif_device->dent_tsif,
2578 &tsif_device->stat_overflow);
2579
2580 debugfs_create_u32(
2581 "stat_lost_sync",
2582 S_IRUGO|S_IWUGO,
2583 tsif_device->dent_tsif,
2584 &tsif_device->stat_lost_sync);
2585
2586 debugfs_create_u32(
2587 "stat_timeout",
2588 S_IRUGO|S_IWUGO,
2589 tsif_device->dent_tsif,
2590 &tsif_device->stat_timeout);
2591
Joel Nider5556a852011-10-16 10:52:13 +02002592 }
2593}
2594
2595static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2596{
2597 if (tsif_device->dent_tsif) {
2598 int i;
2599 debugfs_remove_recursive(tsif_device->dent_tsif);
2600 tsif_device->dent_tsif = NULL;
2601 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2602 tsif_device->debugfs_tsif_regs[i] = NULL;
2603 }
2604}
2605
2606static void tspp_debugfs_init(struct tspp_device *device, int instance)
2607{
2608 char name[10];
2609 snprintf(name, 10, "tspp%i", instance);
2610 device->dent = debugfs_create_dir(
2611 name, NULL);
2612 if (device->dent) {
2613 int i;
2614 void __iomem *base = device->base;
2615 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2616 device->debugfs_regs[i] =
2617 debugfs_create_file(
2618 debugfs_tspp_regs[i].name,
2619 debugfs_tspp_regs[i].mode,
2620 device->dent,
2621 base + debugfs_tspp_regs[i].offset,
2622 &fops_iomem_x32);
2623 }
2624 }
2625}
2626
2627static void tspp_debugfs_exit(struct tspp_device *device)
2628{
2629 if (device->dent) {
2630 int i;
2631 debugfs_remove_recursive(device->dent);
2632 device->dent = NULL;
2633 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2634 device->debugfs_regs[i] = NULL;
2635 }
2636}
Joel Nider5556a852011-10-16 10:52:13 +02002637
Liron Kuch59339922013-01-01 18:29:47 +02002638/* copy device-tree data to platfrom data struct */
2639static __devinit struct msm_tspp_platform_data *
2640msm_tspp_dt_to_pdata(struct platform_device *pdev)
2641{
2642 struct device_node *node = pdev->dev.of_node;
2643 struct msm_tspp_platform_data *data;
2644 struct msm_gpio *gpios;
2645 int i, rc;
2646 int gpio;
2647 u32 gpio_func;
2648
2649 /* Note: memory allocated by devm_kzalloc is freed automatically */
2650 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2651 if (!data) {
2652 pr_err("tspp: Unable to allocate platform data\n");
2653 return NULL;
2654 }
2655 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2656 if (rc) {
2657 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2658 rc);
2659 return NULL;
2660 }
2661 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2662 &data->tsif_ref_clk);
2663 if (rc) {
2664 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2665 rc);
2666 return NULL;
2667 }
2668
2669 data->num_gpios = of_gpio_count(node);
2670 if (data->num_gpios == 0) {
2671 pr_err("tspp: Could not find GPIO definitions\n");
2672 return NULL;
2673 }
2674 gpios = devm_kzalloc(&pdev->dev,
2675 (data->num_gpios * sizeof(struct msm_gpio)),
2676 GFP_KERNEL);
2677 if (!gpios) {
2678 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2679 return NULL;
2680 }
2681 /* Assuming GPIO FUNC property is the same for all GPIOs */
2682 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2683 pr_err("tspp: Could not find gpios-func property\n");
2684 return NULL;
2685 }
2686 for (i = 0; i < data->num_gpios; i++) {
2687 gpio = of_get_gpio(node, i);
2688 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2689 GPIO_CFG_INPUT,
2690 GPIO_CFG_PULL_DOWN,
2691 GPIO_CFG_2MA);
2692 rc = of_property_read_string_index(node, "qcom,gpio-names",
2693 i, &gpios[i].label);
2694 if (rc)
2695 pr_warn("tspp: Could not find gpio-names property\n");
2696 }
2697
2698 data->gpios = gpios;
2699
2700 return data;
2701}
2702
2703static int msm_tspp_map_irqs(struct platform_device *pdev,
2704 struct tspp_device *device)
2705{
2706 int rc;
2707 int i;
2708
2709 /* get IRQ numbers from platform information */
2710
2711 /* map TSPP IRQ */
2712 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2713 if (rc > 0) {
2714 device->tspp_irq = rc;
2715 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2716 dev_name(&pdev->dev), device);
2717 if (rc) {
2718 dev_err(&pdev->dev,
2719 "failed to request TSPP IRQ %d : %d",
2720 device->tspp_irq, rc);
2721 device->tspp_irq = 0;
2722 return -EINVAL;
2723 }
2724 } else {
2725 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2726 return -EINVAL;
2727 }
2728
2729 /* map TSIF IRQs */
2730 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2731 if (rc > 0) {
2732 device->tsif[0].tsif_irq = rc;
2733 } else {
2734 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2735 return -EINVAL;
2736 }
2737
2738 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2739 if (rc > 0) {
2740 device->tsif[1].tsif_irq = rc;
2741 } else {
2742 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2743 return -EINVAL;
2744 }
2745
2746 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2747 rc = request_irq(device->tsif[i].tsif_irq,
2748 tsif_isr, IRQF_SHARED,
2749 dev_name(&pdev->dev), &device->tsif[i]);
2750 if (rc) {
2751 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2752 i, rc);
2753 device->tsif[i].tsif_irq = 0;
2754 }
2755 }
2756
2757 /* map BAM IRQ */
2758 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2759 if (rc > 0) {
2760 device->bam_irq = rc;
2761 } else {
2762 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2763 return -EINVAL;
2764 }
2765
2766 return 0;
2767}
2768
Joel Nider5556a852011-10-16 10:52:13 +02002769static int __devinit msm_tspp_probe(struct platform_device *pdev)
2770{
2771 int rc = -ENODEV;
2772 u32 version;
Liron Kuch72b78552012-10-30 17:47:50 +02002773 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002774 struct msm_tspp_platform_data *data;
2775 struct tspp_device *device;
2776 struct resource *mem_tsif0;
2777 struct resource *mem_tsif1;
2778 struct resource *mem_tspp;
2779 struct resource *mem_bam;
Liron Kuch72b78552012-10-30 17:47:50 +02002780 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002781
Liron Kuch59339922013-01-01 18:29:47 +02002782 if (pdev->dev.of_node) {
2783 /* get information from device tree */
2784 data = msm_tspp_dt_to_pdata(pdev);
2785 /* get device ID */
2786 rc = of_property_read_u32(pdev->dev.of_node,
2787 "cell-index", &pdev->id);
2788 if (rc)
2789 pdev->id = -1;
2790
2791 pdev->dev.platform_data = data;
2792 } else {
2793 /* must have platform data */
2794 data = pdev->dev.platform_data;
2795 }
Joel Nider5556a852011-10-16 10:52:13 +02002796 if (!data) {
2797 pr_err("tspp: Platform data not available");
2798 rc = -EINVAL;
2799 goto out;
2800 }
2801
2802 /* check for valid device id */
Joel Nider435ad8e2011-12-14 16:53:30 +02002803 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002804 pr_err("tspp: Invalid device ID %d", pdev->id);
2805 rc = -EINVAL;
2806 goto out;
2807 }
2808
2809 /* OK, we will use this device */
2810 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2811 if (!device) {
2812 pr_err("tspp: Failed to allocate memory for device");
2813 rc = -ENOMEM;
2814 goto out;
2815 }
2816
2817 /* set up references */
2818 device->pdev = pdev;
2819 platform_set_drvdata(pdev, device);
2820
2821 /* map clocks */
2822 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002823 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002824 if (IS_ERR(device->tsif_pclk)) {
2825 pr_err("tspp: failed to get %s",
2826 data->tsif_pclk);
2827 rc = PTR_ERR(device->tsif_pclk);
2828 device->tsif_pclk = NULL;
2829 goto err_pclock;
2830 }
2831 }
2832 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002833 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002834 if (IS_ERR(device->tsif_ref_clk)) {
2835 pr_err("tspp: failed to get %s",
2836 data->tsif_ref_clk);
2837 rc = PTR_ERR(device->tsif_ref_clk);
2838 device->tsif_ref_clk = NULL;
2839 goto err_refclock;
2840 }
2841 }
2842
2843 /* map I/O memory */
Liron Kuch59339922013-01-01 18:29:47 +02002844 mem_tsif0 = platform_get_resource_byname(pdev,
2845 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002846 if (!mem_tsif0) {
2847 pr_err("tspp: Missing tsif0 MEM resource");
2848 rc = -ENXIO;
2849 goto err_res_tsif0;
2850 }
2851 device->tsif[0].base = ioremap(mem_tsif0->start,
2852 resource_size(mem_tsif0));
2853 if (!device->tsif[0].base) {
2854 pr_err("tspp: ioremap failed");
2855 goto err_map_tsif0;
2856 }
2857
Liron Kuch59339922013-01-01 18:29:47 +02002858 mem_tsif1 = platform_get_resource_byname(pdev,
2859 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002860 if (!mem_tsif1) {
2861 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2862 rc = -ENXIO;
2863 goto err_res_tsif1;
2864 }
2865 device->tsif[1].base = ioremap(mem_tsif1->start,
2866 resource_size(mem_tsif1));
2867 if (!device->tsif[1].base) {
2868 dev_err(&pdev->dev, "ioremap failed");
2869 goto err_map_tsif1;
2870 }
2871
Liron Kuch59339922013-01-01 18:29:47 +02002872 mem_tspp = platform_get_resource_byname(pdev,
2873 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002874 if (!mem_tspp) {
2875 dev_err(&pdev->dev, "Missing MEM resource");
2876 rc = -ENXIO;
2877 goto err_res_dev;
2878 }
2879 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2880 if (!device->base) {
2881 dev_err(&pdev->dev, "ioremap failed");
2882 goto err_map_dev;
2883 }
2884
Liron Kuch59339922013-01-01 18:29:47 +02002885 mem_bam = platform_get_resource_byname(pdev,
2886 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002887 if (!mem_bam) {
2888 pr_err("tspp: Missing bam MEM resource");
2889 rc = -ENXIO;
2890 goto err_res_bam;
2891 }
2892 memset(&device->bam_props, 0, sizeof(device->bam_props));
2893 device->bam_props.phys_addr = mem_bam->start;
2894 device->bam_props.virt_addr = ioremap(mem_bam->start,
2895 resource_size(mem_bam));
2896 if (!device->bam_props.virt_addr) {
2897 dev_err(&pdev->dev, "ioremap failed");
2898 goto err_map_bam;
2899 }
2900
Liron Kuch59339922013-01-01 18:29:47 +02002901 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002902 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002903
Joel Nider5556a852011-10-16 10:52:13 +02002904 /* power management */
2905 pm_runtime_set_active(&pdev->dev);
2906 pm_runtime_enable(&pdev->dev);
2907
Joel Nider5556a852011-10-16 10:52:13 +02002908 tspp_debugfs_init(device, 0);
2909
2910 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2911 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002912
2913 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2914 dev_name(&pdev->dev));
2915
2916 /* set up pointers to ram-based 'registers' */
Joel Nider435ad8e2011-12-14 16:53:30 +02002917 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2918 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2919 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2920 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2921 device->tspp_global_performance =
2922 device->base + TSPP_GLOBAL_PERFORMANCE;
2923 device->tspp_pipe_context =
2924 device->base + TSPP_PIPE_CONTEXT;
2925 device->tspp_pipe_performance =
2926 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002927
2928 device->bam_props.summing_threshold = 0x10;
2929 device->bam_props.irq = device->bam_irq;
2930 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2931
Liron Kuch59339922013-01-01 18:29:47 +02002932 if (tspp_clock_start(device) != 0) {
2933 dev_err(&pdev->dev, "Can't start clocks");
2934 goto err_clock;
2935 }
2936
Joel Nider5556a852011-10-16 10:52:13 +02002937 if (sps_register_bam_device(&device->bam_props,
2938 &device->bam_handle) != 0) {
2939 pr_err("tspp: failed to register bam");
2940 goto err_bam;
2941 }
2942
Joel Nider5556a852011-10-16 10:52:13 +02002943 spin_lock_init(&device->spinlock);
2944 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2945 (unsigned long)device);
2946
2947 /* initialize everything to a known state */
2948 tspp_global_reset(device);
2949
2950 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch59339922013-01-01 18:29:47 +02002951 /*
2952 * TSPP version can be bits [7:0] or alternatively,
2953 * TSPP major version is bits [31:28].
2954 */
2955 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002956 pr_warn("tspp: unrecognized hw version=%i", version);
2957
Joel Nider435ad8e2011-12-14 16:53:30 +02002958 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002959 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider435ad8e2011-12-14 16:53:30 +02002960 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2961 pr_err("tspp_channel_init failed");
2962 goto err_channel;
2963 }
Joel Nider5556a852011-10-16 10:52:13 +02002964 }
2965
Joel Nider435ad8e2011-12-14 16:53:30 +02002966 /* stop the clocks for power savings */
2967 tspp_clock_stop(device);
2968
2969 /* everything is ok, so add the device to the list */
2970 list_add_tail(&(device->devlist), &tspp_devices);
2971
Joel Nider5556a852011-10-16 10:52:13 +02002972 return 0;
2973
Joel Nider435ad8e2011-12-14 16:53:30 +02002974err_channel:
Liron Kuch59339922013-01-01 18:29:47 +02002975 /* un-initialize channels */
Liron Kuch72b78552012-10-30 17:47:50 +02002976 for (j = 0; j < i; j++) {
2977 channel = &(device->channels[i]);
2978 device_destroy(tspp_class, channel->cdev.dev);
2979 cdev_del(&channel->cdev);
2980 }
Liron Kuch59339922013-01-01 18:29:47 +02002981
Joel Nider5556a852011-10-16 10:52:13 +02002982 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02002983err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02002984err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02002985 tspp_debugfs_exit(device);
2986 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2987 tsif_debugfs_exit(&device->tsif[i]);
Joel Nider5556a852011-10-16 10:52:13 +02002988err_irq:
Liron Kuch59339922013-01-01 18:29:47 +02002989 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2990 if (device->tsif[i].tsif_irq)
2991 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
2992 }
2993 if (device->tspp_irq)
2994 free_irq(device->tspp_irq, device);
2995
Joel Nider5556a852011-10-16 10:52:13 +02002996 iounmap(device->bam_props.virt_addr);
2997err_map_bam:
2998err_res_bam:
2999 iounmap(device->base);
3000err_map_dev:
3001err_res_dev:
3002 iounmap(device->tsif[1].base);
3003err_map_tsif1:
3004err_res_tsif1:
3005 iounmap(device->tsif[0].base);
3006err_map_tsif0:
3007err_res_tsif0:
3008 if (device->tsif_ref_clk)
3009 clk_put(device->tsif_ref_clk);
3010err_refclock:
3011 if (device->tsif_pclk)
3012 clk_put(device->tsif_pclk);
3013err_pclock:
3014 kfree(device);
3015
3016out:
3017 return rc;
3018}
3019
3020static int __devexit msm_tspp_remove(struct platform_device *pdev)
3021{
Joel Nider435ad8e2011-12-14 16:53:30 +02003022 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02003023 u32 i;
Joel Nider5556a852011-10-16 10:52:13 +02003024
3025 struct tspp_device *device = platform_get_drvdata(pdev);
3026
Joel Nider435ad8e2011-12-14 16:53:30 +02003027 /* free the buffers, and delete the channels */
3028 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
3029 channel = &device->channels[i];
3030 tspp_close_channel(device->pdev->id, i);
3031 device_destroy(tspp_class, channel->cdev.dev);
3032 cdev_del(&channel->cdev);
3033 }
3034
Liron Kuch59339922013-01-01 18:29:47 +02003035 /* de-registering BAM device requires clocks */
3036 tspp_clock_start(device);
Joel Nider5556a852011-10-16 10:52:13 +02003037 sps_deregister_bam_device(device->bam_handle);
Liron Kuch59339922013-01-01 18:29:47 +02003038 tspp_clock_stop(device);
Joel Nider5556a852011-10-16 10:52:13 +02003039
Hamad Kadmany44307d32012-11-25 09:49:51 +02003040 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02003041 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmany44307d32012-11-25 09:49:51 +02003042 if (device->tsif[i].tsif_irq)
3043 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
3044 }
Joel Nider5556a852011-10-16 10:52:13 +02003045
3046 wake_lock_destroy(&device->wake_lock);
3047 free_irq(device->tspp_irq, device);
Joel Nider5556a852011-10-16 10:52:13 +02003048
3049 iounmap(device->bam_props.virt_addr);
3050 iounmap(device->base);
3051 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
3052 iounmap(device->tsif[i].base);
3053
3054 if (device->tsif_ref_clk)
3055 clk_put(device->tsif_ref_clk);
3056
3057 if (device->tsif_pclk)
3058 clk_put(device->tsif_pclk);
3059
3060 pm_runtime_disable(&pdev->dev);
3061 pm_runtime_put(&pdev->dev);
3062 kfree(device);
3063
3064 return 0;
3065}
3066
3067/*** power management ***/
3068
3069static int tspp_runtime_suspend(struct device *dev)
3070{
3071 dev_dbg(dev, "pm_runtime: suspending...");
3072 return 0;
3073}
3074
3075static int tspp_runtime_resume(struct device *dev)
3076{
3077 dev_dbg(dev, "pm_runtime: resuming...");
3078 return 0;
3079}
3080
3081static const struct dev_pm_ops tspp_dev_pm_ops = {
3082 .runtime_suspend = tspp_runtime_suspend,
3083 .runtime_resume = tspp_runtime_resume,
3084};
3085
Liron Kuch59339922013-01-01 18:29:47 +02003086static struct of_device_id msm_match_table[] = {
3087 {.compatible = "qcom,msm_tspp"},
3088 {}
3089};
3090
Joel Nider5556a852011-10-16 10:52:13 +02003091static struct platform_driver msm_tspp_driver = {
3092 .probe = msm_tspp_probe,
3093 .remove = __exit_p(msm_tspp_remove),
3094 .driver = {
3095 .name = "msm_tspp",
3096 .pm = &tspp_dev_pm_ops,
Liron Kuch59339922013-01-01 18:29:47 +02003097 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003098 },
3099};
3100
3101
3102static int __init mod_init(void)
3103{
Joel Nider5556a852011-10-16 10:52:13 +02003104 int rc;
3105
Joel Nider435ad8e2011-12-14 16:53:30 +02003106 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003107 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3108 if (rc) {
3109 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3110 goto err_devrgn;
3111 }
3112
3113 tspp_class = class_create(THIS_MODULE, "tspp");
3114 if (IS_ERR(tspp_class)) {
3115 rc = PTR_ERR(tspp_class);
3116 pr_err("tspp: Error creating class: %d", rc);
3117 goto err_class;
3118 }
3119
Joel Nider435ad8e2011-12-14 16:53:30 +02003120 /* register the driver, and check hardware */
3121 rc = platform_driver_register(&msm_tspp_driver);
3122 if (rc) {
3123 pr_err("tspp: platform_driver_register failed: %d", rc);
3124 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003125 }
3126
3127 return 0;
3128
Joel Nider435ad8e2011-12-14 16:53:30 +02003129err_register:
3130 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003131err_class:
3132 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3133err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003134 return rc;
3135}
3136
3137static void __exit mod_exit(void)
3138{
Joel Nider435ad8e2011-12-14 16:53:30 +02003139 /* delete low level driver */
3140 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003141
Joel Nider435ad8e2011-12-14 16:53:30 +02003142 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003143 class_destroy(tspp_class);
3144 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003145}
3146
3147module_init(mod_init);
3148module_exit(mod_exit);
3149
Joel Nider435ad8e2011-12-14 16:53:30 +02003150MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003151MODULE_LICENSE("GPL v2");